1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/module.h>
23#include <linux/parport.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
26#include <linux/sched/signal.h>
27
28#include <asm/current.h>
29#include <linux/uaccess.h>
30
31#undef DEBUG
32
33static struct daisydev {
34 struct daisydev *next;
35 struct parport *port;
36 int daisy;
37 int devnum;
38} *topology = NULL;
39static DEFINE_SPINLOCK(topology_lock);
40
41static int numdevs;
42static bool daisy_init_done;
43
44
45static int mux_present(struct parport *port);
46static int num_mux_ports(struct parport *port);
47static int select_port(struct parport *port);
48static int assign_addrs(struct parport *port);
49
50
51static void add_dev(int devnum, struct parport *port, int daisy)
52{
53 struct daisydev *newdev, **p;
54 newdev = kmalloc(sizeof(struct daisydev), GFP_KERNEL);
55 if (newdev) {
56 newdev->port = port;
57 newdev->daisy = daisy;
58 newdev->devnum = devnum;
59 spin_lock(&topology_lock);
60 for (p = &topology; *p && (*p)->devnum<devnum; p = &(*p)->next)
61 ;
62 newdev->next = *p;
63 *p = newdev;
64 spin_unlock(&topology_lock);
65 }
66}
67
68
69static struct parport *clone_parport(struct parport *real, int muxport)
70{
71 struct parport *extra = parport_register_port(real->base,
72 real->irq,
73 real->dma,
74 real->ops);
75 if (extra) {
76 extra->portnum = real->portnum;
77 extra->physport = real;
78 extra->muxport = muxport;
79 real->slaves[muxport-1] = extra;
80 }
81
82 return extra;
83}
84
85static int daisy_drv_probe(struct pardevice *par_dev)
86{
87 struct device_driver *drv = par_dev->dev.driver;
88
89 if (strcmp(drv->name, "daisy_drv"))
90 return -ENODEV;
91 if (strcmp(par_dev->name, daisy_dev_name))
92 return -ENODEV;
93
94 return 0;
95}
96
97static struct parport_driver daisy_driver = {
98 .name = "daisy_drv",
99 .probe = daisy_drv_probe,
100 .devmodel = true,
101};
102
103
104
105int parport_daisy_init(struct parport *port)
106{
107 int detected = 0;
108 char *deviceid;
109 static const char *th[] = { "th", "st", "nd", "rd", "th" };
110 int num_ports;
111 int i;
112 int last_try = 0;
113
114 if (!daisy_init_done) {
115
116
117
118
119
120
121
122 daisy_init_done = true;
123 i = parport_register_driver(&daisy_driver);
124 if (i) {
125 pr_err("daisy registration failed\n");
126 daisy_init_done = false;
127 return i;
128 }
129 }
130
131again:
132
133
134
135
136
137 if (port->muxport < 0 && mux_present(port) &&
138
139 ((num_ports = num_mux_ports(port)) == 2 || num_ports == 4)) {
140
141 port->muxport = 0;
142 pr_info("%s: 1st (default) port of %d-way multiplexor\n",
143 port->name, num_ports);
144 for (i = 1; i < num_ports; i++) {
145
146 struct parport *extra = clone_parport(port, i);
147 if (!extra) {
148 if (signal_pending(current))
149 break;
150
151 schedule();
152 continue;
153 }
154
155 pr_info("%s: %d%s port of %d-way multiplexor on %s\n",
156 extra->name, i + 1, th[i + 1], num_ports,
157 port->name);
158
159
160
161
162 parport_daisy_init(extra);
163 }
164 }
165
166 if (port->muxport >= 0)
167 select_port(port);
168
169 parport_daisy_deselect_all(port);
170 detected += assign_addrs(port);
171
172
173 add_dev(numdevs++, port, -1);
174
175
176 deviceid = kmalloc(1024, GFP_KERNEL);
177 if (deviceid) {
178 if (parport_device_id(numdevs - 1, deviceid, 1024) > 2)
179 detected++;
180
181 kfree(deviceid);
182 }
183
184 if (!detected && !last_try) {
185
186
187
188 parport_daisy_fini(port);
189 parport_write_control(port, PARPORT_CONTROL_SELECT);
190 udelay(50);
191 parport_write_control(port,
192 PARPORT_CONTROL_SELECT |
193 PARPORT_CONTROL_INIT);
194 udelay(50);
195 last_try = 1;
196 goto again;
197 }
198
199 return detected;
200}
201
202
203void parport_daisy_fini(struct parport *port)
204{
205 struct daisydev **p;
206
207 spin_lock(&topology_lock);
208 p = &topology;
209 while (*p) {
210 struct daisydev *dev = *p;
211 if (dev->port != port) {
212 p = &dev->next;
213 continue;
214 }
215 *p = dev->next;
216 kfree(dev);
217 }
218
219
220
221
222 if (!topology) numdevs = 0;
223 spin_unlock(&topology_lock);
224 return;
225}
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241struct pardevice *parport_open(int devnum, const char *name)
242{
243 struct daisydev *p = topology;
244 struct pardev_cb par_cb;
245 struct parport *port;
246 struct pardevice *dev;
247 int daisy;
248
249 memset(&par_cb, 0, sizeof(par_cb));
250 spin_lock(&topology_lock);
251 while (p && p->devnum != devnum)
252 p = p->next;
253
254 if (!p) {
255 spin_unlock(&topology_lock);
256 return NULL;
257 }
258
259 daisy = p->daisy;
260 port = parport_get_port(p->port);
261 spin_unlock(&topology_lock);
262
263 dev = parport_register_dev_model(port, name, &par_cb, devnum);
264 parport_put_port(port);
265 if (!dev)
266 return NULL;
267
268 dev->daisy = daisy;
269
270
271 if (daisy >= 0) {
272 int selected;
273 parport_claim_or_block(dev);
274 selected = port->daisy;
275 parport_release(dev);
276
277 if (selected != daisy) {
278
279 parport_unregister_device(dev);
280 return NULL;
281 }
282 }
283
284 return dev;
285}
286
287
288
289
290
291
292
293
294
295void parport_close(struct pardevice *dev)
296{
297 parport_unregister_device(dev);
298}
299
300
301static int cpp_daisy(struct parport *port, int cmd)
302{
303 unsigned char s;
304
305 parport_data_forward(port);
306 parport_write_data(port, 0xaa); udelay(2);
307 parport_write_data(port, 0x55); udelay(2);
308 parport_write_data(port, 0x00); udelay(2);
309 parport_write_data(port, 0xff); udelay(2);
310 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
311 | PARPORT_STATUS_PAPEROUT
312 | PARPORT_STATUS_SELECT
313 | PARPORT_STATUS_ERROR);
314 if (s != (PARPORT_STATUS_BUSY
315 | PARPORT_STATUS_PAPEROUT
316 | PARPORT_STATUS_SELECT
317 | PARPORT_STATUS_ERROR)) {
318 pr_debug("%s: cpp_daisy: aa5500ff(%02x)\n", port->name, s);
319 return -ENXIO;
320 }
321
322 parport_write_data(port, 0x87); udelay(2);
323 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
324 | PARPORT_STATUS_PAPEROUT
325 | PARPORT_STATUS_SELECT
326 | PARPORT_STATUS_ERROR);
327 if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
328 pr_debug("%s: cpp_daisy: aa5500ff87(%02x)\n", port->name, s);
329 return -ENXIO;
330 }
331
332 parport_write_data(port, 0x78); udelay(2);
333 parport_write_data(port, cmd); udelay(2);
334 parport_frob_control(port,
335 PARPORT_CONTROL_STROBE,
336 PARPORT_CONTROL_STROBE);
337 udelay(1);
338 s = parport_read_status(port);
339 parport_frob_control(port, PARPORT_CONTROL_STROBE, 0);
340 udelay(1);
341 parport_write_data(port, 0xff); udelay(2);
342
343 return s;
344}
345
346
347static int cpp_mux(struct parport *port, int cmd)
348{
349 unsigned char s;
350 int rc;
351
352 parport_data_forward(port);
353 parport_write_data(port, 0xaa); udelay(2);
354 parport_write_data(port, 0x55); udelay(2);
355 parport_write_data(port, 0xf0); udelay(2);
356 parport_write_data(port, 0x0f); udelay(2);
357 parport_write_data(port, 0x52); udelay(2);
358 parport_write_data(port, 0xad); udelay(2);
359 parport_write_data(port, cmd); udelay(2);
360
361 s = parport_read_status(port);
362 if (!(s & PARPORT_STATUS_ACK)) {
363 pr_debug("%s: cpp_mux: aa55f00f52ad%02x(%02x)\n",
364 port->name, cmd, s);
365 return -EIO;
366 }
367
368 rc = (((s & PARPORT_STATUS_SELECT ? 1 : 0) << 0) |
369 ((s & PARPORT_STATUS_PAPEROUT ? 1 : 0) << 1) |
370 ((s & PARPORT_STATUS_BUSY ? 0 : 1) << 2) |
371 ((s & PARPORT_STATUS_ERROR ? 0 : 1) << 3));
372
373 return rc;
374}
375
376void parport_daisy_deselect_all(struct parport *port)
377{
378 cpp_daisy(port, 0x30);
379}
380
381int parport_daisy_select(struct parport *port, int daisy, int mode)
382{
383 switch (mode)
384 {
385
386 case IEEE1284_MODE_EPP:
387 case IEEE1284_MODE_EPPSL:
388 case IEEE1284_MODE_EPPSWE:
389 return !(cpp_daisy(port, 0x20 + daisy) &
390 PARPORT_STATUS_ERROR);
391
392
393 case IEEE1284_MODE_ECP:
394 case IEEE1284_MODE_ECPRLE:
395 case IEEE1284_MODE_ECPSWE:
396 return !(cpp_daisy(port, 0xd0 + daisy) &
397 PARPORT_STATUS_ERROR);
398
399
400
401 case IEEE1284_MODE_BECP:
402
403 case IEEE1284_MODE_NIBBLE:
404 case IEEE1284_MODE_BYTE:
405 case IEEE1284_MODE_COMPAT:
406 default:
407 return !(cpp_daisy(port, 0xe0 + daisy) &
408 PARPORT_STATUS_ERROR);
409 }
410}
411
412static int mux_present(struct parport *port)
413{
414 return cpp_mux(port, 0x51) == 3;
415}
416
417static int num_mux_ports(struct parport *port)
418{
419 return cpp_mux(port, 0x58);
420}
421
422static int select_port(struct parport *port)
423{
424 int muxport = port->muxport;
425 return cpp_mux(port, 0x60 + muxport) == muxport;
426}
427
428static int assign_addrs(struct parport *port)
429{
430 unsigned char s;
431 unsigned char daisy;
432 int thisdev = numdevs;
433 int detected;
434 char *deviceid;
435
436 parport_data_forward(port);
437 parport_write_data(port, 0xaa); udelay(2);
438 parport_write_data(port, 0x55); udelay(2);
439 parport_write_data(port, 0x00); udelay(2);
440 parport_write_data(port, 0xff); udelay(2);
441 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
442 | PARPORT_STATUS_PAPEROUT
443 | PARPORT_STATUS_SELECT
444 | PARPORT_STATUS_ERROR);
445 if (s != (PARPORT_STATUS_BUSY
446 | PARPORT_STATUS_PAPEROUT
447 | PARPORT_STATUS_SELECT
448 | PARPORT_STATUS_ERROR)) {
449 pr_debug("%s: assign_addrs: aa5500ff(%02x)\n", port->name, s);
450 return 0;
451 }
452
453 parport_write_data(port, 0x87); udelay(2);
454 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
455 | PARPORT_STATUS_PAPEROUT
456 | PARPORT_STATUS_SELECT
457 | PARPORT_STATUS_ERROR);
458 if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
459 pr_debug("%s: assign_addrs: aa5500ff87(%02x)\n", port->name, s);
460 return 0;
461 }
462
463 parport_write_data(port, 0x78); udelay(2);
464 s = parport_read_status(port);
465
466 for (daisy = 0;
467 (s & (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT))
468 == (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT)
469 && daisy < 4;
470 ++daisy) {
471 parport_write_data(port, daisy);
472 udelay(2);
473 parport_frob_control(port,
474 PARPORT_CONTROL_STROBE,
475 PARPORT_CONTROL_STROBE);
476 udelay(1);
477 parport_frob_control(port, PARPORT_CONTROL_STROBE, 0);
478 udelay(1);
479
480 add_dev(numdevs++, port, daisy);
481
482
483
484 if (!(s & PARPORT_STATUS_BUSY))
485 break;
486
487
488
489
490
491 s = parport_read_status(port);
492 }
493
494 parport_write_data(port, 0xff); udelay(2);
495 detected = numdevs - thisdev;
496 pr_debug("%s: Found %d daisy-chained devices\n", port->name, detected);
497
498
499 deviceid = kmalloc(1024, GFP_KERNEL);
500 if (!deviceid) return 0;
501
502 for (daisy = 0; thisdev < numdevs; thisdev++, daisy++)
503 parport_device_id(thisdev, deviceid, 1024);
504
505 kfree(deviceid);
506 return detected;
507}
508