blob: 5695e00219e438bb8e69268d29ac7c8d63ed5944 [file] [log] [blame]
Geoff Levandffbdd242007-06-16 08:05:53 +10001/*
2 * PS3 device registration routines.
3 *
4 * Copyright (C) 2007 Sony Computer Entertainment Inc.
5 * Copyright 2007 Sony Corp.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/delay.h>
22#include <linux/freezer.h>
23#include <linux/kernel.h>
24#include <linux/kthread.h>
25#include <linux/init.h>
Geert Uytterhoevenb4cb2942008-01-19 07:30:40 +110026#include <linux/reboot.h>
Geoff Levandffbdd242007-06-16 08:05:53 +100027
28#include <asm/firmware.h>
29#include <asm/lv1call.h>
Geert Uytterhoevena5c631b2007-06-22 00:14:22 +100030#include <asm/ps3stor.h>
Geoff Levandffbdd242007-06-16 08:05:53 +100031
32#include "platform.h"
33
34/**
35 * ps3_setup_gelic_device - Setup and register a gelic device instance.
36 *
37 * Allocates memory for a struct ps3_system_bus_device instance, initialises the
38 * structure members, and registers the device instance with the system bus.
39 */
40
41static int __init ps3_setup_gelic_device(
42 const struct ps3_repository_device *repo)
43{
44 int result;
45 struct layout {
46 struct ps3_system_bus_device dev;
47 struct ps3_dma_region d_region;
48 } *p;
49
50 pr_debug(" -> %s:%d\n", __func__, __LINE__);
51
52 BUG_ON(repo->bus_type != PS3_BUS_TYPE_SB);
53 BUG_ON(repo->dev_type != PS3_DEV_TYPE_SB_GELIC);
54
55 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
56
57 if (!p) {
58 result = -ENOMEM;
59 goto fail_malloc;
60 }
61
62 p->dev.match_id = PS3_MATCH_ID_GELIC;
63 p->dev.dev_type = PS3_DEVICE_TYPE_SB;
64 p->dev.bus_id = repo->bus_id;
65 p->dev.dev_id = repo->dev_id;
66 p->dev.d_region = &p->d_region;
67
68 result = ps3_repository_find_interrupt(repo,
69 PS3_INTERRUPT_TYPE_EVENT_PORT, &p->dev.interrupt_id);
70
71 if (result) {
72 pr_debug("%s:%d ps3_repository_find_interrupt failed\n",
73 __func__, __LINE__);
74 goto fail_find_interrupt;
75 }
76
77 BUG_ON(p->dev.interrupt_id != 0);
78
79 result = ps3_dma_region_init(&p->dev, p->dev.d_region, PS3_DMA_64K,
80 PS3_DMA_OTHER, NULL, 0);
81
82 if (result) {
83 pr_debug("%s:%d ps3_dma_region_init failed\n",
84 __func__, __LINE__);
85 goto fail_dma_init;
86 }
87
88 result = ps3_system_bus_device_register(&p->dev);
89
90 if (result) {
91 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
92 __func__, __LINE__);
93 goto fail_device_register;
94 }
95
96 pr_debug(" <- %s:%d\n", __func__, __LINE__);
97 return result;
98
99fail_device_register:
100fail_dma_init:
101fail_find_interrupt:
102 kfree(p);
103fail_malloc:
104 pr_debug(" <- %s:%d: fail.\n", __func__, __LINE__);
105 return result;
106}
107
108static int __init_refok ps3_setup_uhc_device(
109 const struct ps3_repository_device *repo, enum ps3_match_id match_id,
110 enum ps3_interrupt_type interrupt_type, enum ps3_reg_type reg_type)
111{
112 int result;
113 struct layout {
114 struct ps3_system_bus_device dev;
115 struct ps3_dma_region d_region;
116 struct ps3_mmio_region m_region;
117 } *p;
118 u64 bus_addr;
119 u64 len;
120
121 pr_debug(" -> %s:%d\n", __func__, __LINE__);
122
123 BUG_ON(repo->bus_type != PS3_BUS_TYPE_SB);
124 BUG_ON(repo->dev_type != PS3_DEV_TYPE_SB_USB);
125
126 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
127
128 if (!p) {
129 result = -ENOMEM;
130 goto fail_malloc;
131 }
132
133 p->dev.match_id = match_id;
134 p->dev.dev_type = PS3_DEVICE_TYPE_SB;
135 p->dev.bus_id = repo->bus_id;
136 p->dev.dev_id = repo->dev_id;
137 p->dev.d_region = &p->d_region;
138 p->dev.m_region = &p->m_region;
139
140 result = ps3_repository_find_interrupt(repo,
141 interrupt_type, &p->dev.interrupt_id);
142
143 if (result) {
144 pr_debug("%s:%d ps3_repository_find_interrupt failed\n",
145 __func__, __LINE__);
146 goto fail_find_interrupt;
147 }
148
149 result = ps3_repository_find_reg(repo, reg_type,
150 &bus_addr, &len);
151
152 if (result) {
153 pr_debug("%s:%d ps3_repository_find_reg failed\n",
154 __func__, __LINE__);
155 goto fail_find_reg;
156 }
157
158 result = ps3_dma_region_init(&p->dev, p->dev.d_region, PS3_DMA_64K,
159 PS3_DMA_INTERNAL, NULL, 0);
160
161 if (result) {
162 pr_debug("%s:%d ps3_dma_region_init failed\n",
163 __func__, __LINE__);
164 goto fail_dma_init;
165 }
166
167 result = ps3_mmio_region_init(&p->dev, p->dev.m_region, bus_addr, len,
168 PS3_MMIO_4K);
169
170 if (result) {
171 pr_debug("%s:%d ps3_mmio_region_init failed\n",
172 __func__, __LINE__);
173 goto fail_mmio_init;
174 }
175
176 result = ps3_system_bus_device_register(&p->dev);
177
178 if (result) {
179 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
180 __func__, __LINE__);
181 goto fail_device_register;
182 }
183
184 pr_debug(" <- %s:%d\n", __func__, __LINE__);
185 return result;
186
187fail_device_register:
188fail_mmio_init:
189fail_dma_init:
190fail_find_reg:
191fail_find_interrupt:
192 kfree(p);
193fail_malloc:
194 pr_debug(" <- %s:%d: fail.\n", __func__, __LINE__);
195 return result;
196}
197
198static int __init ps3_setup_ehci_device(
199 const struct ps3_repository_device *repo)
200{
201 return ps3_setup_uhc_device(repo, PS3_MATCH_ID_EHCI,
202 PS3_INTERRUPT_TYPE_SB_EHCI, PS3_REG_TYPE_SB_EHCI);
203}
204
205static int __init ps3_setup_ohci_device(
206 const struct ps3_repository_device *repo)
207{
208 return ps3_setup_uhc_device(repo, PS3_MATCH_ID_OHCI,
209 PS3_INTERRUPT_TYPE_SB_OHCI, PS3_REG_TYPE_SB_OHCI);
210}
211
212static int __init ps3_setup_vuart_device(enum ps3_match_id match_id,
213 unsigned int port_number)
214{
215 int result;
216 struct layout {
217 struct ps3_system_bus_device dev;
218 } *p;
219
220 pr_debug(" -> %s:%d: match_id %u, port %u\n", __func__, __LINE__,
221 match_id, port_number);
222
223 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
224
225 if (!p)
226 return -ENOMEM;
227
228 p->dev.match_id = match_id;
229 p->dev.dev_type = PS3_DEVICE_TYPE_VUART;
230 p->dev.port_number = port_number;
231
232 result = ps3_system_bus_device_register(&p->dev);
233
234 if (result)
235 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
236 __func__, __LINE__);
237
238 pr_debug(" <- %s:%d\n", __func__, __LINE__);
239 return result;
240}
241
Geert Uytterhoevena5c631b2007-06-22 00:14:22 +1000242static int ps3_setup_storage_dev(const struct ps3_repository_device *repo,
243 enum ps3_match_id match_id)
244{
245 int result;
246 struct ps3_storage_device *p;
247 u64 port, blk_size, num_blocks;
248 unsigned int num_regions, i;
249
250 pr_debug(" -> %s:%u: match_id %u\n", __func__, __LINE__, match_id);
251
252 result = ps3_repository_read_stor_dev_info(repo->bus_index,
253 repo->dev_index, &port,
254 &blk_size, &num_blocks,
255 &num_regions);
256 if (result) {
257 printk(KERN_ERR "%s:%u: _read_stor_dev_info failed %d\n",
258 __func__, __LINE__, result);
259 return -ENODEV;
260 }
261
Geoff Levand75cdff92007-09-12 18:43:17 +1000262 pr_debug("%s:%u: (%u:%u:%u): port %lu blk_size %lu num_blocks %lu "
Geert Uytterhoevena5c631b2007-06-22 00:14:22 +1000263 "num_regions %u\n", __func__, __LINE__, repo->bus_index,
Geoff Levand75cdff92007-09-12 18:43:17 +1000264 repo->dev_index, repo->dev_type, port, blk_size, num_blocks,
265 num_regions);
Geert Uytterhoevena5c631b2007-06-22 00:14:22 +1000266
267 p = kzalloc(sizeof(struct ps3_storage_device) +
268 num_regions * sizeof(struct ps3_storage_region),
269 GFP_KERNEL);
270 if (!p) {
271 result = -ENOMEM;
272 goto fail_malloc;
273 }
274
275 p->sbd.match_id = match_id;
276 p->sbd.dev_type = PS3_DEVICE_TYPE_SB;
277 p->sbd.bus_id = repo->bus_id;
278 p->sbd.dev_id = repo->dev_id;
279 p->sbd.d_region = &p->dma_region;
280 p->blk_size = blk_size;
281 p->num_regions = num_regions;
282
283 result = ps3_repository_find_interrupt(repo,
284 PS3_INTERRUPT_TYPE_EVENT_PORT,
285 &p->sbd.interrupt_id);
286 if (result) {
287 printk(KERN_ERR "%s:%u: find_interrupt failed %d\n", __func__,
288 __LINE__, result);
289 result = -ENODEV;
290 goto fail_find_interrupt;
291 }
292
Geert Uytterhoevena5c631b2007-06-22 00:14:22 +1000293 for (i = 0; i < num_regions; i++) {
294 unsigned int id;
295 u64 start, size;
296
297 result = ps3_repository_read_stor_dev_region(repo->bus_index,
298 repo->dev_index,
299 i, &id, &start,
300 &size);
301 if (result) {
302 printk(KERN_ERR
303 "%s:%u: read_stor_dev_region failed %d\n",
304 __func__, __LINE__, result);
305 result = -ENODEV;
306 goto fail_read_region;
307 }
308 pr_debug("%s:%u: region %u: id %u start %lu size %lu\n",
309 __func__, __LINE__, i, id, start, size);
310
311 p->regions[i].id = id;
312 p->regions[i].start = start;
313 p->regions[i].size = size;
314 }
315
316 result = ps3_system_bus_device_register(&p->sbd);
317 if (result) {
318 pr_debug("%s:%u ps3_system_bus_device_register failed\n",
319 __func__, __LINE__);
320 goto fail_device_register;
321 }
322
323 pr_debug(" <- %s:%u\n", __func__, __LINE__);
324 return 0;
325
326fail_device_register:
327fail_read_region:
Geert Uytterhoevena5c631b2007-06-22 00:14:22 +1000328fail_find_interrupt:
329 kfree(p);
330fail_malloc:
331 pr_debug(" <- %s:%u: fail.\n", __func__, __LINE__);
332 return result;
333}
334
Geoff Levandffbdd242007-06-16 08:05:53 +1000335static int __init ps3_register_vuart_devices(void)
336{
337 int result;
338 unsigned int port_number;
339
340 pr_debug(" -> %s:%d\n", __func__, __LINE__);
341
342 result = ps3_repository_read_vuart_av_port(&port_number);
343 if (result)
344 port_number = 0; /* av default */
345
346 result = ps3_setup_vuart_device(PS3_MATCH_ID_AV_SETTINGS, port_number);
347 WARN_ON(result);
348
349 result = ps3_repository_read_vuart_sysmgr_port(&port_number);
350 if (result)
351 port_number = 2; /* sysmgr default */
352
353 result = ps3_setup_vuart_device(PS3_MATCH_ID_SYSTEM_MANAGER,
354 port_number);
355 WARN_ON(result);
356
357 pr_debug(" <- %s:%d\n", __func__, __LINE__);
358 return result;
359}
360
361static int __init ps3_register_sound_devices(void)
362{
363 int result;
364 struct layout {
365 struct ps3_system_bus_device dev;
366 struct ps3_dma_region d_region;
367 struct ps3_mmio_region m_region;
368 } *p;
369
370 pr_debug(" -> %s:%d\n", __func__, __LINE__);
371
372 p = kzalloc(sizeof(*p), GFP_KERNEL);
373 if (!p)
374 return -ENOMEM;
375
376 p->dev.match_id = PS3_MATCH_ID_SOUND;
377 p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
378 p->dev.d_region = &p->d_region;
379 p->dev.m_region = &p->m_region;
380
381 result = ps3_system_bus_device_register(&p->dev);
382
383 if (result)
384 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
385 __func__, __LINE__);
386
387 pr_debug(" <- %s:%d\n", __func__, __LINE__);
388 return result;
389}
390
391static int __init ps3_register_graphics_devices(void)
392{
393 int result;
394 struct layout {
395 struct ps3_system_bus_device dev;
396 } *p;
397
398 pr_debug(" -> %s:%d\n", __func__, __LINE__);
399
400 p = kzalloc(sizeof(struct layout), GFP_KERNEL);
401
402 if (!p)
403 return -ENOMEM;
404
405 p->dev.match_id = PS3_MATCH_ID_GRAPHICS;
406 p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
407
408 result = ps3_system_bus_device_register(&p->dev);
409
410 if (result)
411 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
412 __func__, __LINE__);
413
414 pr_debug(" <- %s:%d\n", __func__, __LINE__);
415 return result;
416}
417
418/**
419 * ps3_register_repository_device - Register a device from the repositiory info.
420 *
421 */
422
423static int ps3_register_repository_device(
424 const struct ps3_repository_device *repo)
425{
426 int result;
427
428 switch (repo->dev_type) {
429 case PS3_DEV_TYPE_SB_GELIC:
430 result = ps3_setup_gelic_device(repo);
431 if (result) {
432 pr_debug("%s:%d ps3_setup_gelic_device failed\n",
433 __func__, __LINE__);
434 }
435 break;
436 case PS3_DEV_TYPE_SB_USB:
437
438 /* Each USB device has both an EHCI and an OHCI HC */
439
440 result = ps3_setup_ehci_device(repo);
441
442 if (result) {
443 pr_debug("%s:%d ps3_setup_ehci_device failed\n",
444 __func__, __LINE__);
445 }
446
447 result = ps3_setup_ohci_device(repo);
448
449 if (result) {
450 pr_debug("%s:%d ps3_setup_ohci_device failed\n",
451 __func__, __LINE__);
452 }
453 break;
Geert Uytterhoevena5c631b2007-06-22 00:14:22 +1000454 case PS3_DEV_TYPE_STOR_DISK:
455 result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_DISK);
456
457 /* Some devices are not accessable from the Other OS lpar. */
458 if (result == -ENODEV) {
459 result = 0;
460 pr_debug("%s:%u: not accessable\n", __func__,
461 __LINE__);
462 }
463
464 if (result)
465 pr_debug("%s:%u ps3_setup_storage_dev failed\n",
466 __func__, __LINE__);
467 break;
468
469 case PS3_DEV_TYPE_STOR_ROM:
470 result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_ROM);
471 if (result)
472 pr_debug("%s:%u ps3_setup_storage_dev failed\n",
473 __func__, __LINE__);
474 break;
475
476 case PS3_DEV_TYPE_STOR_FLASH:
477 result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_FLASH);
478 if (result)
479 pr_debug("%s:%u ps3_setup_storage_dev failed\n",
480 __func__, __LINE__);
481 break;
482
Geoff Levandffbdd242007-06-16 08:05:53 +1000483 default:
484 result = 0;
485 pr_debug("%s:%u: unsupported dev_type %u\n", __func__, __LINE__,
486 repo->dev_type);
487 }
488
489 return result;
490}
491
Geert Uytterhoevenb4cb2942008-01-19 07:30:40 +1100492
493#define PS3_NOTIFICATION_DEV_ID ULONG_MAX
494#define PS3_NOTIFICATION_INTERRUPT_ID 0
495
496struct ps3_notification_device {
497 struct ps3_system_bus_device sbd;
498 spinlock_t lock;
499 u64 tag;
500 u64 lv1_status;
501 struct completion done;
502};
503
504enum ps3_notify_type {
505 notify_device_ready = 0,
506 notify_region_probe = 1,
507 notify_region_update = 2,
508};
509
510struct ps3_notify_cmd {
511 u64 operation_code; /* must be zero */
512 u64 event_mask; /* OR of 1UL << enum ps3_notify_type */
513};
514
515struct ps3_notify_event {
516 u64 event_type; /* enum ps3_notify_type */
517 u64 bus_id;
518 u64 dev_id;
519 u64 dev_type;
520 u64 dev_port;
521};
522
523static irqreturn_t ps3_notification_interrupt(int irq, void *data)
524{
525 struct ps3_notification_device *dev = data;
526 int res;
527 u64 tag, status;
528
529 spin_lock(&dev->lock);
530 res = lv1_storage_get_async_status(PS3_NOTIFICATION_DEV_ID, &tag,
531 &status);
532 if (tag != dev->tag)
533 pr_err("%s:%u: tag mismatch, got %lx, expected %lx\n",
534 __func__, __LINE__, tag, dev->tag);
535
536 if (res) {
537 pr_err("%s:%u: res %d status 0x%lx\n", __func__, __LINE__, res,
538 status);
539 } else {
540 pr_debug("%s:%u: completed, status 0x%lx\n", __func__,
541 __LINE__, status);
542 dev->lv1_status = status;
543 complete(&dev->done);
544 }
545 spin_unlock(&dev->lock);
546 return IRQ_HANDLED;
547}
548
549static int ps3_notification_read_write(struct ps3_notification_device *dev,
550 u64 lpar, int write)
551{
552 const char *op = write ? "write" : "read";
553 unsigned long flags;
554 int res;
555
556 init_completion(&dev->done);
557 spin_lock_irqsave(&dev->lock, flags);
558 res = write ? lv1_storage_write(dev->sbd.dev_id, 0, 0, 1, 0, lpar,
559 &dev->tag)
560 : lv1_storage_read(dev->sbd.dev_id, 0, 0, 1, 0, lpar,
561 &dev->tag);
562 spin_unlock_irqrestore(&dev->lock, flags);
563 if (res) {
564 pr_err("%s:%u: %s failed %d\n", __func__, __LINE__, op, res);
565 return -EPERM;
566 }
567 pr_debug("%s:%u: notification %s issued\n", __func__, __LINE__, op);
568
569 res = wait_event_interruptible(dev->done.wait,
570 dev->done.done || kthread_should_stop());
571 if (kthread_should_stop())
572 res = -EINTR;
573 if (res) {
574 pr_debug("%s:%u: interrupted %s\n", __func__, __LINE__, op);
575 return res;
576 }
577
578 if (dev->lv1_status) {
579 pr_err("%s:%u: %s not completed, status 0x%lx\n", __func__,
580 __LINE__, op, dev->lv1_status);
581 return -EIO;
582 }
583 pr_debug("%s:%u: notification %s completed\n", __func__, __LINE__, op);
584
585 return 0;
586}
587
588static struct task_struct *probe_task;
589
Geoff Levandffbdd242007-06-16 08:05:53 +1000590/**
591 * ps3_probe_thread - Background repository probing at system startup.
592 *
593 * This implementation only supports background probing on a single bus.
Geert Uytterhoevenb4cb2942008-01-19 07:30:40 +1100594 * It uses the hypervisor's storage device notification mechanism to wait until
595 * a storage device is ready. The device notification mechanism uses a
596 * pseudo device to asynchronously notify the guest when storage devices become
597 * ready. The notification device has a block size of 512 bytes.
Geoff Levandffbdd242007-06-16 08:05:53 +1000598 */
599
600static int ps3_probe_thread(void *data)
601{
Geert Uytterhoevenb4cb2942008-01-19 07:30:40 +1100602 struct ps3_notification_device dev;
603 struct ps3_repository_device repo;
604 int res;
605 unsigned int irq;
606 u64 lpar;
607 void *buf;
608 struct ps3_notify_cmd *notify_cmd;
609 struct ps3_notify_event *notify_event;
Geoff Levandffbdd242007-06-16 08:05:53 +1000610
611 pr_debug(" -> %s:%u: kthread started\n", __func__, __LINE__);
612
Geert Uytterhoevenb4cb2942008-01-19 07:30:40 +1100613 buf = kzalloc(512, GFP_KERNEL);
614 if (!buf)
615 return -ENOMEM;
616
617 lpar = ps3_mm_phys_to_lpar(__pa(buf));
618 notify_cmd = buf;
619 notify_event = buf;
620
621 /* dummy system bus device */
622 dev.sbd.bus_id = (u64)data;
623 dev.sbd.dev_id = PS3_NOTIFICATION_DEV_ID;
624 dev.sbd.interrupt_id = PS3_NOTIFICATION_INTERRUPT_ID;
625
626 res = lv1_open_device(dev.sbd.bus_id, dev.sbd.dev_id, 0);
627 if (res) {
628 pr_err("%s:%u: lv1_open_device failed %s\n", __func__,
629 __LINE__, ps3_result(res));
630 goto fail_free;
631 }
632
633 res = ps3_sb_event_receive_port_setup(&dev.sbd, PS3_BINDING_CPU_ANY,
634 &irq);
635 if (res) {
636 pr_err("%s:%u: ps3_sb_event_receive_port_setup failed %d\n",
637 __func__, __LINE__, res);
638 goto fail_close_device;
639 }
640
641 spin_lock_init(&dev.lock);
642
643 res = request_irq(irq, ps3_notification_interrupt, IRQF_DISABLED,
644 "ps3_notification", &dev);
645 if (res) {
646 pr_err("%s:%u: request_irq failed %d\n", __func__, __LINE__,
647 res);
648 goto fail_sb_event_receive_port_destroy;
649 }
650
651 /* Setup and write the request for device notification. */
652 notify_cmd->operation_code = 0; /* must be zero */
653 notify_cmd->event_mask = 1UL << notify_region_probe;
654
655 res = ps3_notification_read_write(&dev, lpar, 1);
656 if (res)
657 goto fail_free_irq;
658
659 /* Loop here processing the requested notification events. */
Geoff Levandffbdd242007-06-16 08:05:53 +1000660 do {
661 try_to_freeze();
662
Geert Uytterhoevenb4cb2942008-01-19 07:30:40 +1100663 memset(notify_event, 0, sizeof(*notify_event));
Geoff Levandffbdd242007-06-16 08:05:53 +1000664
Geert Uytterhoevenb4cb2942008-01-19 07:30:40 +1100665 res = ps3_notification_read_write(&dev, lpar, 0);
666 if (res)
Geoff Levandffbdd242007-06-16 08:05:53 +1000667 break;
668
Geert Uytterhoevenb4cb2942008-01-19 07:30:40 +1100669 pr_debug("%s:%u: notify event type 0x%lx bus id %lu dev id %lu"
670 " type %lu port %lu\n", __func__, __LINE__,
671 notify_event->event_type, notify_event->bus_id,
672 notify_event->dev_id, notify_event->dev_type,
673 notify_event->dev_port);
Geoff Levandffbdd242007-06-16 08:05:53 +1000674
Geert Uytterhoevenb4cb2942008-01-19 07:30:40 +1100675 if (notify_event->event_type != notify_region_probe ||
676 notify_event->bus_id != dev.sbd.bus_id) {
677 pr_warning("%s:%u: bad notify_event: event %lu, "
678 "dev_id %lu, dev_type %lu\n",
679 __func__, __LINE__, notify_event->event_type,
680 notify_event->dev_id,
681 notify_event->dev_type);
682 continue;
683 }
684
685 res = ps3_repository_find_device_by_id(&repo, dev.sbd.bus_id,
686 notify_event->dev_id);
687 if (res) {
688 pr_warning("%s:%u: device %lu:%lu not found\n",
689 __func__, __LINE__, dev.sbd.bus_id,
690 notify_event->dev_id);
691 continue;
692 }
693
694 pr_debug("%s:%u: device %lu:%lu found\n", __func__, __LINE__,
695 dev.sbd.bus_id, notify_event->dev_id);
696 ps3_register_repository_device(&repo);
Geoff Levandffbdd242007-06-16 08:05:53 +1000697
698 } while (!kthread_should_stop());
699
Geert Uytterhoevenb4cb2942008-01-19 07:30:40 +1100700fail_free_irq:
701 free_irq(irq, &dev);
702fail_sb_event_receive_port_destroy:
703 ps3_sb_event_receive_port_destroy(&dev.sbd, irq);
704fail_close_device:
705 lv1_close_device(dev.sbd.bus_id, dev.sbd.dev_id);
706fail_free:
707 kfree(buf);
708
709 probe_task = NULL;
710
Geoff Levandffbdd242007-06-16 08:05:53 +1000711 pr_debug(" <- %s:%u: kthread finished\n", __func__, __LINE__);
712
713 return 0;
714}
715
716/**
Geert Uytterhoevenb4cb2942008-01-19 07:30:40 +1100717 * ps3_stop_probe_thread - Stops the background probe thread.
718 *
719 */
720
721static int ps3_stop_probe_thread(struct notifier_block *nb, unsigned long code,
722 void *data)
723{
724 if (probe_task)
725 kthread_stop(probe_task);
726 return 0;
727}
728
729static struct notifier_block nb = {
730 .notifier_call = ps3_stop_probe_thread
731};
732
733/**
Geoff Levandffbdd242007-06-16 08:05:53 +1000734 * ps3_start_probe_thread - Starts the background probe thread.
735 *
736 */
737
738static int __init ps3_start_probe_thread(enum ps3_bus_type bus_type)
739{
740 int result;
741 struct task_struct *task;
Geert Uytterhoevenb4cb2942008-01-19 07:30:40 +1100742 struct ps3_repository_device repo;
Geoff Levandffbdd242007-06-16 08:05:53 +1000743
744 pr_debug(" -> %s:%d\n", __func__, __LINE__);
745
746 memset(&repo, 0, sizeof(repo));
747
748 repo.bus_type = bus_type;
749
750 result = ps3_repository_find_bus(repo.bus_type, 0, &repo.bus_index);
751
752 if (result) {
753 printk(KERN_ERR "%s: Cannot find bus (%d)\n", __func__, result);
754 return -ENODEV;
755 }
756
757 result = ps3_repository_read_bus_id(repo.bus_index, &repo.bus_id);
758
759 if (result) {
760 printk(KERN_ERR "%s: read_bus_id failed %d\n", __func__,
761 result);
762 return -ENODEV;
763 }
764
Geert Uytterhoevenb4cb2942008-01-19 07:30:40 +1100765 task = kthread_run(ps3_probe_thread, (void *)repo.bus_id,
766 "ps3-probe-%u", bus_type);
Geoff Levandffbdd242007-06-16 08:05:53 +1000767
768 if (IS_ERR(task)) {
769 result = PTR_ERR(task);
770 printk(KERN_ERR "%s: kthread_run failed %d\n", __func__,
771 result);
772 return result;
773 }
774
Geert Uytterhoevenb4cb2942008-01-19 07:30:40 +1100775 probe_task = task;
776 register_reboot_notifier(&nb);
777
Geoff Levandffbdd242007-06-16 08:05:53 +1000778 pr_debug(" <- %s:%d\n", __func__, __LINE__);
779 return 0;
780}
781
782/**
783 * ps3_register_devices - Probe the system and register devices found.
784 *
785 * A device_initcall() routine.
786 */
787
788static int __init ps3_register_devices(void)
789{
790 int result;
791
792 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
793 return -ENODEV;
794
795 pr_debug(" -> %s:%d\n", __func__, __LINE__);
796
797 /* ps3_repository_dump_bus_info(); */
798
799 result = ps3_start_probe_thread(PS3_BUS_TYPE_STORAGE);
800
801 ps3_register_vuart_devices();
802
803 ps3_register_graphics_devices();
804
805 ps3_repository_find_devices(PS3_BUS_TYPE_SB,
806 ps3_register_repository_device);
807
808 ps3_register_sound_devices();
809
810 pr_debug(" <- %s:%d\n", __func__, __LINE__);
811 return 0;
812}
813
814device_initcall(ps3_register_devices);