blob: 746298107d6fa97d5611f05a0e33d58b717281ed [file] [log] [blame]
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001/*
2 * PS3 virtual uart
3 *
4 * Copyright (C) 2006 Sony Computer Entertainment Inc.
5 * Copyright 2006 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/kernel.h>
22#include <linux/module.h>
23#include <linux/interrupt.h>
Geoff Levandea1547d2007-02-06 14:23:47 -080024#include <linux/workqueue.h>
Geoff Levand74e95d5d2006-12-08 18:27:47 -080025#include <asm/ps3.h>
26
Geoff Levand75c86e72007-02-13 17:37:28 -080027#include <asm/firmware.h>
Geoff Levand74e95d5d2006-12-08 18:27:47 -080028#include <asm/lv1call.h>
29#include <asm/bitops.h>
30
31#include "vuart.h"
32
33MODULE_AUTHOR("Sony Corporation");
34MODULE_LICENSE("GPL v2");
Geoff Levand75c86e72007-02-13 17:37:28 -080035MODULE_DESCRIPTION("PS3 vuart");
Geoff Levand74e95d5d2006-12-08 18:27:47 -080036
37/**
38 * vuart - An inter-partition data link service.
39 * port 0: PS3 AV Settings.
40 * port 2: PS3 System Manager.
41 *
42 * The vuart provides a bi-directional byte stream data link between logical
43 * partitions. Its primary role is as a communications link between the guest
44 * OS and the system policy module. The current HV does not support any
45 * connections other than those listed.
46 */
47
48enum {PORT_COUNT = 3,};
49
50enum vuart_param {
51 PARAM_TX_TRIGGER = 0,
52 PARAM_RX_TRIGGER = 1,
53 PARAM_INTERRUPT_MASK = 2,
54 PARAM_RX_BUF_SIZE = 3, /* read only */
55 PARAM_RX_BYTES = 4, /* read only */
56 PARAM_TX_BUF_SIZE = 5, /* read only */
57 PARAM_TX_BYTES = 6, /* read only */
58 PARAM_INTERRUPT_STATUS = 7, /* read only */
59};
60
61enum vuart_interrupt_bit {
62 INTERRUPT_BIT_TX = 0,
63 INTERRUPT_BIT_RX = 1,
64 INTERRUPT_BIT_DISCONNECT = 2,
65};
66
67enum vuart_interrupt_mask {
68 INTERRUPT_MASK_TX = 1,
69 INTERRUPT_MASK_RX = 2,
70 INTERRUPT_MASK_DISCONNECT = 4,
71};
72
73/**
74 * struct ports_bmp - bitmap indicating ports needing service.
75 *
76 * A 256 bit read only bitmap indicating ports needing service. Do not write
77 * to these bits. Must not cross a page boundary.
78 */
79
80struct ports_bmp {
81 u64 status;
82 u64 unused[3];
83} __attribute__ ((aligned (32)));
84
85/* redefine dev_dbg to do a syntax check */
86
87#if !defined(DEBUG)
88#undef dev_dbg
89static inline int __attribute__ ((format (printf, 2, 3))) dev_dbg(
90 const struct device *_dev, const char *fmt, ...) {return 0;}
91#endif
92
93#define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__)
94static void __attribute__ ((unused)) _dump_ports_bmp(
95 const struct ports_bmp* bmp, const char* func, int line)
96{
97 pr_debug("%s:%d: ports_bmp: %016lxh\n", func, line, bmp->status);
98}
99
100static int ps3_vuart_match_id_to_port(enum ps3_match_id match_id,
101 unsigned int *port_number)
102{
103 switch(match_id) {
104 case PS3_MATCH_ID_AV_SETTINGS:
105 *port_number = 0;
106 return 0;
107 case PS3_MATCH_ID_SYSTEM_MANAGER:
108 *port_number = 2;
109 return 0;
110 default:
111 WARN_ON(1);
112 *port_number = UINT_MAX;
113 return -EINVAL;
114 };
115}
116
117#define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__)
118static void __attribute__ ((unused)) _dump_port_params(unsigned int port_number,
119 const char* func, int line)
120{
121#if defined(DEBUG)
122 static const char *strings[] = {
123 "tx_trigger ",
124 "rx_trigger ",
125 "interrupt_mask ",
126 "rx_buf_size ",
127 "rx_bytes ",
128 "tx_buf_size ",
129 "tx_bytes ",
130 "interrupt_status",
131 };
132 int result;
133 unsigned int i;
134 u64 value;
135
136 for (i = 0; i < ARRAY_SIZE(strings); i++) {
137 result = lv1_get_virtual_uart_param(port_number, i, &value);
138
139 if (result) {
140 pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line,
141 port_number, strings[i], ps3_result(result));
142 continue;
143 }
144 pr_debug("%s:%d: port_%u: %s = %lxh\n",
145 func, line, port_number, strings[i], value);
146 }
147#endif
148}
149
150struct vuart_triggers {
151 unsigned long rx;
152 unsigned long tx;
153};
154
155int ps3_vuart_get_triggers(struct ps3_vuart_port_device *dev,
156 struct vuart_triggers *trig)
157{
158 int result;
159 unsigned long size;
160 unsigned long val;
161
Geoff Levand75c86e72007-02-13 17:37:28 -0800162 result = lv1_get_virtual_uart_param(dev->priv->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800163 PARAM_TX_TRIGGER, &trig->tx);
164
165 if (result) {
166 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
167 __func__, __LINE__, ps3_result(result));
168 return result;
169 }
170
Geoff Levand75c86e72007-02-13 17:37:28 -0800171 result = lv1_get_virtual_uart_param(dev->priv->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800172 PARAM_RX_BUF_SIZE, &size);
173
174 if (result) {
175 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
176 __func__, __LINE__, ps3_result(result));
177 return result;
178 }
179
Geoff Levand75c86e72007-02-13 17:37:28 -0800180 result = lv1_get_virtual_uart_param(dev->priv->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800181 PARAM_RX_TRIGGER, &val);
182
183 if (result) {
184 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
185 __func__, __LINE__, ps3_result(result));
186 return result;
187 }
188
189 trig->rx = size - val;
190
191 dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__,
192 trig->tx, trig->rx);
193
194 return result;
195}
196
197int ps3_vuart_set_triggers(struct ps3_vuart_port_device *dev, unsigned int tx,
198 unsigned int rx)
199{
200 int result;
201 unsigned long size;
202
Geoff Levand75c86e72007-02-13 17:37:28 -0800203 result = lv1_set_virtual_uart_param(dev->priv->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800204 PARAM_TX_TRIGGER, tx);
205
206 if (result) {
207 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
208 __func__, __LINE__, ps3_result(result));
209 return result;
210 }
211
Geoff Levand75c86e72007-02-13 17:37:28 -0800212 result = lv1_get_virtual_uart_param(dev->priv->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800213 PARAM_RX_BUF_SIZE, &size);
214
215 if (result) {
216 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
217 __func__, __LINE__, ps3_result(result));
218 return result;
219 }
220
Geoff Levand75c86e72007-02-13 17:37:28 -0800221 result = lv1_set_virtual_uart_param(dev->priv->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800222 PARAM_RX_TRIGGER, size - rx);
223
224 if (result) {
225 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
226 __func__, __LINE__, ps3_result(result));
227 return result;
228 }
229
230 dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__,
231 tx, rx);
232
233 return result;
234}
235
236static int ps3_vuart_get_rx_bytes_waiting(struct ps3_vuart_port_device *dev,
Geoff Levand75c86e72007-02-13 17:37:28 -0800237 u64 *bytes_waiting)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800238{
Geoff Levand75c86e72007-02-13 17:37:28 -0800239 int result = lv1_get_virtual_uart_param(dev->priv->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800240 PARAM_RX_BYTES, bytes_waiting);
241
242 if (result)
243 dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n",
244 __func__, __LINE__, ps3_result(result));
245
246 dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__,
247 *bytes_waiting);
248 return result;
249}
250
251static int ps3_vuart_set_interrupt_mask(struct ps3_vuart_port_device *dev,
252 unsigned long mask)
253{
254 int result;
255
256 dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, mask);
257
Geoff Levand75c86e72007-02-13 17:37:28 -0800258 dev->priv->interrupt_mask = mask;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800259
Geoff Levand75c86e72007-02-13 17:37:28 -0800260 result = lv1_set_virtual_uart_param(dev->priv->port_number,
261 PARAM_INTERRUPT_MASK, dev->priv->interrupt_mask);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800262
263 if (result)
264 dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n",
265 __func__, __LINE__, ps3_result(result));
266
267 return result;
268}
269
Geoff Levand75c86e72007-02-13 17:37:28 -0800270static int ps3_vuart_get_interrupt_status(struct ps3_vuart_port_device *dev,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800271 unsigned long *status)
272{
Geoff Levand75c86e72007-02-13 17:37:28 -0800273 u64 tmp;
274 int result = lv1_get_virtual_uart_param(dev->priv->port_number,
275 PARAM_INTERRUPT_STATUS, &tmp);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800276
277 if (result)
278 dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n",
279 __func__, __LINE__, ps3_result(result));
280
Geoff Levand75c86e72007-02-13 17:37:28 -0800281 *status = tmp & dev->priv->interrupt_mask;
282
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800283 dev_dbg(&dev->core, "%s:%d: m %lxh, s %lxh, m&s %lxh\n",
Geoff Levand75c86e72007-02-13 17:37:28 -0800284 __func__, __LINE__, dev->priv->interrupt_mask, tmp, *status);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800285
286 return result;
287}
288
289int ps3_vuart_enable_interrupt_tx(struct ps3_vuart_port_device *dev)
290{
Geoff Levand75c86e72007-02-13 17:37:28 -0800291 return (dev->priv->interrupt_mask & INTERRUPT_MASK_TX) ? 0
292 : ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800293 | INTERRUPT_MASK_TX);
294}
295
296int ps3_vuart_enable_interrupt_rx(struct ps3_vuart_port_device *dev)
297{
Geoff Levand75c86e72007-02-13 17:37:28 -0800298 return (dev->priv->interrupt_mask & INTERRUPT_MASK_RX) ? 0
299 : ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800300 | INTERRUPT_MASK_RX);
301}
302
303int ps3_vuart_enable_interrupt_disconnect(struct ps3_vuart_port_device *dev)
304{
Geoff Levand75c86e72007-02-13 17:37:28 -0800305 return (dev->priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) ? 0
306 : ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800307 | INTERRUPT_MASK_DISCONNECT);
308}
309
310int ps3_vuart_disable_interrupt_tx(struct ps3_vuart_port_device *dev)
311{
Geoff Levand75c86e72007-02-13 17:37:28 -0800312 return (dev->priv->interrupt_mask & INTERRUPT_MASK_TX)
313 ? ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800314 & ~INTERRUPT_MASK_TX) : 0;
315}
316
317int ps3_vuart_disable_interrupt_rx(struct ps3_vuart_port_device *dev)
318{
Geoff Levand75c86e72007-02-13 17:37:28 -0800319 return (dev->priv->interrupt_mask & INTERRUPT_MASK_RX)
320 ? ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800321 & ~INTERRUPT_MASK_RX) : 0;
322}
323
324int ps3_vuart_disable_interrupt_disconnect(struct ps3_vuart_port_device *dev)
325{
Geoff Levand75c86e72007-02-13 17:37:28 -0800326 return (dev->priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT)
327 ? ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800328 & ~INTERRUPT_MASK_DISCONNECT) : 0;
329}
330
331/**
332 * ps3_vuart_raw_write - Low level write helper.
333 *
334 * Do not call ps3_vuart_raw_write directly, use ps3_vuart_write.
335 */
336
337static int ps3_vuart_raw_write(struct ps3_vuart_port_device *dev,
338 const void* buf, unsigned int bytes, unsigned long *bytes_written)
339{
340 int result;
341
Geoff Levand75c86e72007-02-13 17:37:28 -0800342 result = lv1_write_virtual_uart(dev->priv->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800343 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written);
344
345 if (result) {
346 dev_dbg(&dev->core, "%s:%d: lv1_write_virtual_uart failed: "
347 "%s\n", __func__, __LINE__, ps3_result(result));
348 return result;
349 }
350
Geoff Levand75c86e72007-02-13 17:37:28 -0800351 dev->priv->stats.bytes_written += *bytes_written;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800352
Geoff Levand75c86e72007-02-13 17:37:28 -0800353 dev_dbg(&dev->core, "%s:%d: wrote %lxh/%xh=>%lxh\n", __func__, __LINE__,
354 *bytes_written, bytes, dev->priv->stats.bytes_written);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800355
356 return result;
357}
358
359/**
360 * ps3_vuart_raw_read - Low level read helper.
361 *
362 * Do not call ps3_vuart_raw_read directly, use ps3_vuart_read.
363 */
364
365static int ps3_vuart_raw_read(struct ps3_vuart_port_device *dev, void* buf,
366 unsigned int bytes, unsigned long *bytes_read)
367{
368 int result;
369
370 dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes);
371
Geoff Levand75c86e72007-02-13 17:37:28 -0800372 result = lv1_read_virtual_uart(dev->priv->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800373 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_read);
374
375 if (result) {
376 dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n",
377 __func__, __LINE__, ps3_result(result));
378 return result;
379 }
380
Geoff Levand75c86e72007-02-13 17:37:28 -0800381 dev->priv->stats.bytes_read += *bytes_read;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800382
383 dev_dbg(&dev->core, "%s:%d: read %lxh/%xh=>%lxh\n", __func__, __LINE__,
Geoff Levand75c86e72007-02-13 17:37:28 -0800384 *bytes_read, bytes, dev->priv->stats.bytes_read);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800385
386 return result;
387}
388
389/**
Geoff Levand75c86e72007-02-13 17:37:28 -0800390 * ps3_vuart_clear_rx_bytes - Discard bytes received.
391 * @bytes: Max byte count to discard, zero = all pending.
392 *
393 * Used to clear pending rx interrupt source. Will not block.
394 */
395
396void ps3_vuart_clear_rx_bytes(struct ps3_vuart_port_device *dev,
397 unsigned int bytes)
398{
399 int result;
400 u64 bytes_waiting;
401 void* tmp;
402
403 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting);
404
405 BUG_ON(result);
406
407 bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting;
408
409 dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes);
410
411 if (!bytes)
412 return;
413
414 /* Add some extra space for recently arrived data. */
415
416 bytes += 128;
417
418 tmp = kmalloc(bytes, GFP_KERNEL);
419
420 if (!tmp)
421 return;
422
423 ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting);
424
425 kfree(tmp);
426
427 /* Don't include these bytes in the stats. */
428
429 dev->priv->stats.bytes_read -= bytes_waiting;
430}
431
432/**
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800433 * struct list_buffer - An element for a port device fifo buffer list.
434 */
435
436struct list_buffer {
437 struct list_head link;
438 const unsigned char *head;
439 const unsigned char *tail;
440 unsigned long dbg_number;
441 unsigned char data[];
442};
443
444/**
445 * ps3_vuart_write - the entry point for writing data to a port
446 *
447 * If the port is idle on entry as much of the incoming data is written to
448 * the port as the port will accept. Otherwise a list buffer is created
449 * and any remaning incoming data is copied to that buffer. The buffer is
450 * then enqueued for transmision via the transmit interrupt.
451 */
452
453int ps3_vuart_write(struct ps3_vuart_port_device *dev, const void* buf,
454 unsigned int bytes)
455{
456 static unsigned long dbg_number;
457 int result;
458 unsigned long flags;
459 struct list_buffer *lb;
460
461 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
462 bytes, bytes);
463
Geoff Levand75c86e72007-02-13 17:37:28 -0800464 spin_lock_irqsave(&dev->priv->tx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800465
Geoff Levand75c86e72007-02-13 17:37:28 -0800466 if (list_empty(&dev->priv->tx_list.head)) {
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800467 unsigned long bytes_written;
468
469 result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written);
470
Geoff Levand75c86e72007-02-13 17:37:28 -0800471 spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800472
473 if (result) {
474 dev_dbg(&dev->core,
475 "%s:%d: ps3_vuart_raw_write failed\n",
476 __func__, __LINE__);
477 return result;
478 }
479
480 if (bytes_written == bytes) {
481 dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n",
482 __func__, __LINE__, bytes);
483 return 0;
484 }
485
486 bytes -= bytes_written;
487 buf += bytes_written;
488 } else
Geoff Levand75c86e72007-02-13 17:37:28 -0800489 spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800490
491 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL);
492
493 if (!lb) {
494 return -ENOMEM;
495 }
496
497 memcpy(lb->data, buf, bytes);
498 lb->head = lb->data;
499 lb->tail = lb->data + bytes;
500 lb->dbg_number = ++dbg_number;
501
Geoff Levand75c86e72007-02-13 17:37:28 -0800502 spin_lock_irqsave(&dev->priv->tx_list.lock, flags);
503 list_add_tail(&lb->link, &dev->priv->tx_list.head);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800504 ps3_vuart_enable_interrupt_tx(dev);
Geoff Levand75c86e72007-02-13 17:37:28 -0800505 spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800506
507 dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %xh bytes\n",
508 __func__, __LINE__, lb->dbg_number, bytes);
509
510 return 0;
511}
512
513/**
514 * ps3_vuart_read - the entry point for reading data from a port
515 *
516 * If enough bytes to satisfy the request are held in the buffer list those
517 * bytes are dequeued and copied to the caller's buffer. Emptied list buffers
518 * are retiered. If the request cannot be statified by bytes held in the list
519 * buffers -EAGAIN is returned.
520 */
521
522int ps3_vuart_read(struct ps3_vuart_port_device *dev, void* buf,
523 unsigned int bytes)
524{
525 unsigned long flags;
526 struct list_buffer *lb, *n;
527 unsigned long bytes_read;
528
529 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
530 bytes, bytes);
531
Geoff Levand75c86e72007-02-13 17:37:28 -0800532 spin_lock_irqsave(&dev->priv->rx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800533
Geoff Levand75c86e72007-02-13 17:37:28 -0800534 if (dev->priv->rx_list.bytes_held < bytes) {
535 spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800536 dev_dbg(&dev->core, "%s:%d: starved for %lxh bytes\n",
Geoff Levand75c86e72007-02-13 17:37:28 -0800537 __func__, __LINE__,
538 bytes - dev->priv->rx_list.bytes_held);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800539 return -EAGAIN;
540 }
541
Geoff Levand75c86e72007-02-13 17:37:28 -0800542 list_for_each_entry_safe(lb, n, &dev->priv->rx_list.head, link) {
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800543 bytes_read = min((unsigned int)(lb->tail - lb->head), bytes);
544
545 memcpy(buf, lb->head, bytes_read);
546 buf += bytes_read;
547 bytes -= bytes_read;
Geoff Levand75c86e72007-02-13 17:37:28 -0800548 dev->priv->rx_list.bytes_held -= bytes_read;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800549
550 if (bytes_read < lb->tail - lb->head) {
551 lb->head += bytes_read;
Geoff Levand75c86e72007-02-13 17:37:28 -0800552 dev_dbg(&dev->core, "%s:%d: buf_%lu: dequeued %lxh "
553 "bytes\n", __func__, __LINE__, lb->dbg_number,
554 bytes_read);
555 spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800556 return 0;
557 }
558
Geoff Levand75c86e72007-02-13 17:37:28 -0800559 dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh "
560 "bytes\n", __func__, __LINE__, lb->dbg_number,
561 bytes_read);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800562
563 list_del(&lb->link);
564 kfree(lb);
565 }
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800566
Geoff Levand75c86e72007-02-13 17:37:28 -0800567 spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800568 return 0;
569}
570
Geoff Levandea1547d2007-02-06 14:23:47 -0800571int ps3_vuart_read_async(struct ps3_vuart_port_device *dev, work_func_t func,
572 unsigned int bytes)
573{
574 unsigned long flags;
575
576 if(dev->priv->work.trigger) {
577 dev_dbg(&dev->core, "%s:%d: warning, multiple calls\n",
578 __func__, __LINE__);
579 return -EAGAIN;
580 }
581
582 BUG_ON(!bytes);
583
584 PREPARE_WORK(&dev->priv->work.work, func);
585
586 spin_lock_irqsave(&dev->priv->work.lock, flags);
587 if(dev->priv->rx_list.bytes_held >= bytes) {
588 dev_dbg(&dev->core, "%s:%d: schedule_work %xh bytes\n",
589 __func__, __LINE__, bytes);
590 schedule_work(&dev->priv->work.work);
591 spin_unlock_irqrestore(&dev->priv->work.lock, flags);
592 return 0;
593 }
594
595 dev->priv->work.trigger = bytes;
596 spin_unlock_irqrestore(&dev->priv->work.lock, flags);
597
598 dev_dbg(&dev->core, "%s:%d: waiting for %u(%xh) bytes\n", __func__,
599 __LINE__, bytes, bytes);
600
601 return 0;
602}
603
604void ps3_vuart_cancel_async(struct ps3_vuart_port_device *dev)
605{
606 dev->priv->work.trigger = 0;
607}
608
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800609/**
610 * ps3_vuart_handle_interrupt_tx - third stage transmit interrupt handler
611 *
612 * Services the transmit interrupt for the port. Writes as much data from the
613 * buffer list as the port will accept. Retires any emptied list buffers and
614 * adjusts the final list buffer state for a partial write.
615 */
616
617static int ps3_vuart_handle_interrupt_tx(struct ps3_vuart_port_device *dev)
618{
619 int result = 0;
620 unsigned long flags;
621 struct list_buffer *lb, *n;
622 unsigned long bytes_total = 0;
623
624 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
625
Geoff Levand75c86e72007-02-13 17:37:28 -0800626 spin_lock_irqsave(&dev->priv->tx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800627
Geoff Levand75c86e72007-02-13 17:37:28 -0800628 list_for_each_entry_safe(lb, n, &dev->priv->tx_list.head, link) {
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800629
630 unsigned long bytes_written;
631
632 result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head,
633 &bytes_written);
634
635 if (result) {
636 dev_dbg(&dev->core,
637 "%s:%d: ps3_vuart_raw_write failed\n",
638 __func__, __LINE__);
639 break;
640 }
641
642 bytes_total += bytes_written;
643
644 if (bytes_written < lb->tail - lb->head) {
645 lb->head += bytes_written;
646 dev_dbg(&dev->core,
647 "%s:%d cleared buf_%lu, %lxh bytes\n",
648 __func__, __LINE__, lb->dbg_number,
649 bytes_written);
650 goto port_full;
651 }
652
653 dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__,
654 lb->dbg_number);
655
656 list_del(&lb->link);
657 kfree(lb);
658 }
659
660 ps3_vuart_disable_interrupt_tx(dev);
661port_full:
Geoff Levand75c86e72007-02-13 17:37:28 -0800662 spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800663 dev_dbg(&dev->core, "%s:%d wrote %lxh bytes total\n",
664 __func__, __LINE__, bytes_total);
665 return result;
666}
667
668/**
669 * ps3_vuart_handle_interrupt_rx - third stage receive interrupt handler
670 *
671 * Services the receive interrupt for the port. Creates a list buffer and
672 * copies all waiting port data to that buffer and enqueues the buffer in the
673 * buffer list. Buffer list data is dequeued via ps3_vuart_read.
674 */
675
676static int ps3_vuart_handle_interrupt_rx(struct ps3_vuart_port_device *dev)
677{
678 static unsigned long dbg_number;
679 int result = 0;
680 unsigned long flags;
681 struct list_buffer *lb;
682 unsigned long bytes;
683
684 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
685
686 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes);
687
688 if (result)
689 return -EIO;
690
691 BUG_ON(!bytes);
692
Geoff Levand75c86e72007-02-13 17:37:28 -0800693 /* Add some extra space for recently arrived data. */
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800694
695 bytes += 128;
696
697 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC);
698
699 if (!lb)
700 return -ENOMEM;
701
702 ps3_vuart_raw_read(dev, lb->data, bytes, &bytes);
703
704 lb->head = lb->data;
705 lb->tail = lb->data + bytes;
706 lb->dbg_number = ++dbg_number;
707
Geoff Levand75c86e72007-02-13 17:37:28 -0800708 spin_lock_irqsave(&dev->priv->rx_list.lock, flags);
709 list_add_tail(&lb->link, &dev->priv->rx_list.head);
710 dev->priv->rx_list.bytes_held += bytes;
711 spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800712
Geoff Levand75c86e72007-02-13 17:37:28 -0800713 dev_dbg(&dev->core, "%s:%d: buf_%lu: queued %lxh bytes\n",
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800714 __func__, __LINE__, lb->dbg_number, bytes);
715
Geoff Levandea1547d2007-02-06 14:23:47 -0800716 spin_lock_irqsave(&dev->priv->work.lock, flags);
717 if(dev->priv->work.trigger
718 && dev->priv->rx_list.bytes_held >= dev->priv->work.trigger) {
719 dev_dbg(&dev->core, "%s:%d: schedule_work %lxh bytes\n",
720 __func__, __LINE__, dev->priv->work.trigger);
721 dev->priv->work.trigger = 0;
722 schedule_work(&dev->priv->work.work);
723 }
724 spin_unlock_irqrestore(&dev->priv->work.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800725 return 0;
726}
727
728static int ps3_vuart_handle_interrupt_disconnect(
729 struct ps3_vuart_port_device *dev)
730{
731 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
732 BUG_ON("no support");
733 return -1;
734}
735
736/**
737 * ps3_vuart_handle_port_interrupt - second stage interrupt handler
738 *
739 * Services any pending interrupt types for the port. Passes control to the
740 * third stage type specific interrupt handler. Returns control to the first
741 * stage handler after one iteration.
742 */
743
744static int ps3_vuart_handle_port_interrupt(struct ps3_vuart_port_device *dev)
745{
746 int result;
747 unsigned long status;
748
Geoff Levand75c86e72007-02-13 17:37:28 -0800749 result = ps3_vuart_get_interrupt_status(dev, &status);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800750
751 if (result)
752 return result;
753
754 dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__,
755 status);
756
757 if (status & INTERRUPT_MASK_DISCONNECT) {
Geoff Levand75c86e72007-02-13 17:37:28 -0800758 dev->priv->stats.disconnect_interrupts++;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800759 result = ps3_vuart_handle_interrupt_disconnect(dev);
760 if (result)
761 ps3_vuart_disable_interrupt_disconnect(dev);
762 }
763
764 if (status & INTERRUPT_MASK_TX) {
Geoff Levand75c86e72007-02-13 17:37:28 -0800765 dev->priv->stats.tx_interrupts++;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800766 result = ps3_vuart_handle_interrupt_tx(dev);
767 if (result)
768 ps3_vuart_disable_interrupt_tx(dev);
769 }
770
771 if (status & INTERRUPT_MASK_RX) {
Geoff Levand75c86e72007-02-13 17:37:28 -0800772 dev->priv->stats.rx_interrupts++;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800773 result = ps3_vuart_handle_interrupt_rx(dev);
774 if (result)
775 ps3_vuart_disable_interrupt_rx(dev);
776 }
777
778 return 0;
779}
780
Geoff Levand75c86e72007-02-13 17:37:28 -0800781struct vuart_bus_priv {
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800782 const struct ports_bmp bmp;
Geoff Levand75c86e72007-02-13 17:37:28 -0800783 unsigned int virq;
784 struct semaphore probe_mutex;
785 int use_count;
786 struct ps3_vuart_port_device *devices[PORT_COUNT];
787} static vuart_bus_priv;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800788
789/**
790 * ps3_vuart_irq_handler - first stage interrupt handler
791 *
792 * Loops finding any interrupting port and its associated instance data.
793 * Passes control to the second stage port specific interrupt handler. Loops
794 * until all outstanding interrupts are serviced.
795 */
796
797static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private)
798{
Geoff Levand75c86e72007-02-13 17:37:28 -0800799 struct vuart_bus_priv *bus_priv;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800800
801 BUG_ON(!_private);
Geoff Levand75c86e72007-02-13 17:37:28 -0800802 bus_priv = (struct vuart_bus_priv *)_private;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800803
804 while (1) {
805 unsigned int port;
806
Geoff Levand75c86e72007-02-13 17:37:28 -0800807 dump_ports_bmp(&bus_priv->bmp);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800808
Geoff Levand75c86e72007-02-13 17:37:28 -0800809 port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp.status);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800810
811 if (port == BITS_PER_LONG)
812 break;
813
814 BUG_ON(port >= PORT_COUNT);
Geoff Levand75c86e72007-02-13 17:37:28 -0800815 BUG_ON(!bus_priv->devices[port]);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800816
Geoff Levand75c86e72007-02-13 17:37:28 -0800817 ps3_vuart_handle_port_interrupt(bus_priv->devices[port]);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800818 }
819
820 return IRQ_HANDLED;
821}
822
823static int ps3_vuart_match(struct device *_dev, struct device_driver *_drv)
824{
825 int result;
826 struct ps3_vuart_port_driver *drv = to_ps3_vuart_port_driver(_drv);
827 struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
828
829 result = dev->match_id == drv->match_id;
830
831 dev_info(&dev->core, "%s:%d: dev=%u(%s), drv=%u(%s): %s\n", __func__,
832 __LINE__, dev->match_id, dev->core.bus_id, drv->match_id,
833 drv->core.name, (result ? "match" : "miss"));
834
835 return result;
836}
837
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800838static int ps3_vuart_probe(struct device *_dev)
839{
840 int result;
Geoff Levand75c86e72007-02-13 17:37:28 -0800841 unsigned int port_number;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800842 struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
843 struct ps3_vuart_port_driver *drv =
844 to_ps3_vuart_port_driver(_dev->driver);
845
846 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
847
848 BUG_ON(!drv);
849
Geoff Levand75c86e72007-02-13 17:37:28 -0800850 down(&vuart_bus_priv.probe_mutex);
851
852 /* Setup vuart_bus_priv.devices[]. */
853
854 result = ps3_vuart_match_id_to_port(dev->match_id,
855 &port_number);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800856
857 if (result) {
858 dev_dbg(&dev->core, "%s:%d: unknown match_id (%d)\n",
859 __func__, __LINE__, dev->match_id);
860 result = -EINVAL;
861 goto fail_match;
862 }
863
Geoff Levand75c86e72007-02-13 17:37:28 -0800864 if (vuart_bus_priv.devices[port_number]) {
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800865 dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__,
Geoff Levand75c86e72007-02-13 17:37:28 -0800866 __LINE__, port_number);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800867 result = -EBUSY;
868 goto fail_match;
869 }
870
Geoff Levand75c86e72007-02-13 17:37:28 -0800871 vuart_bus_priv.devices[port_number] = dev;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800872
Geoff Levand75c86e72007-02-13 17:37:28 -0800873 /* Setup dev->priv. */
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800874
Geoff Levand75c86e72007-02-13 17:37:28 -0800875 dev->priv = kzalloc(sizeof(struct ps3_vuart_port_priv), GFP_KERNEL);
876
877 if (!dev->priv) {
878 result = -ENOMEM;
879 goto fail_alloc;
880 }
881
882 dev->priv->port_number = port_number;
883
884 INIT_LIST_HEAD(&dev->priv->tx_list.head);
885 spin_lock_init(&dev->priv->tx_list.lock);
886
887 INIT_LIST_HEAD(&dev->priv->rx_list.head);
888 spin_lock_init(&dev->priv->rx_list.lock);
889
Geoff Levandea1547d2007-02-06 14:23:47 -0800890 INIT_WORK(&dev->priv->work.work, NULL);
891 spin_lock_init(&dev->priv->work.lock);
892 dev->priv->work.trigger = 0;
893 dev->priv->work.dev = dev;
894
Geoff Levand75c86e72007-02-13 17:37:28 -0800895 if (++vuart_bus_priv.use_count == 1) {
896
Geoff Levand861be322007-01-26 19:08:08 -0800897 result = ps3_alloc_vuart_irq(PS3_BINDING_CPU_ANY,
Geoff Levand75c86e72007-02-13 17:37:28 -0800898 (void*)&vuart_bus_priv.bmp.status, &vuart_bus_priv.virq);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800899
900 if (result) {
901 dev_dbg(&dev->core,
902 "%s:%d: ps3_alloc_vuart_irq failed (%d)\n",
903 __func__, __LINE__, result);
904 result = -EPERM;
905 goto fail_alloc_irq;
906 }
907
Geoff Levand75c86e72007-02-13 17:37:28 -0800908 result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler,
909 IRQF_DISABLED, "vuart", &vuart_bus_priv);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800910
911 if (result) {
912 dev_info(&dev->core, "%s:%d: request_irq failed (%d)\n",
913 __func__, __LINE__, result);
914 goto fail_request_irq;
915 }
916 }
917
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800918 /* clear stale pending interrupts */
Geoff Levand75c86e72007-02-13 17:37:28 -0800919
920 ps3_vuart_clear_rx_bytes(dev, 0);
921
922 ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800923
924 ps3_vuart_set_triggers(dev, 1, 1);
925
926 if (drv->probe)
927 result = drv->probe(dev);
928 else {
929 result = 0;
930 dev_info(&dev->core, "%s:%d: no probe method\n", __func__,
931 __LINE__);
932 }
933
934 if (result) {
935 dev_dbg(&dev->core, "%s:%d: drv->probe failed\n",
936 __func__, __LINE__);
Geoff Levand75c86e72007-02-13 17:37:28 -0800937 down(&vuart_bus_priv.probe_mutex);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800938 goto fail_probe;
939 }
940
Geoff Levand75c86e72007-02-13 17:37:28 -0800941 up(&vuart_bus_priv.probe_mutex);
942
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800943 return result;
944
945fail_probe:
Geoff Levand75c86e72007-02-13 17:37:28 -0800946 ps3_vuart_set_interrupt_mask(dev, 0);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800947fail_request_irq:
Geoff Levand75c86e72007-02-13 17:37:28 -0800948 ps3_free_vuart_irq(vuart_bus_priv.virq);
949 vuart_bus_priv.virq = NO_IRQ;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800950fail_alloc_irq:
Geoff Levand75c86e72007-02-13 17:37:28 -0800951 --vuart_bus_priv.use_count;
952 kfree(dev->priv);
953 dev->priv = NULL;
954fail_alloc:
955 vuart_bus_priv.devices[port_number] = 0;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800956fail_match:
Geoff Levand75c86e72007-02-13 17:37:28 -0800957 up(&vuart_bus_priv.probe_mutex);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800958 dev_dbg(&dev->core, "%s:%d failed\n", __func__, __LINE__);
959 return result;
960}
961
962static int ps3_vuart_remove(struct device *_dev)
963{
964 struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
965 struct ps3_vuart_port_driver *drv =
966 to_ps3_vuart_port_driver(_dev->driver);
967
Geoff Levand75c86e72007-02-13 17:37:28 -0800968 down(&vuart_bus_priv.probe_mutex);
969
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800970 dev_dbg(&dev->core, "%s:%d: %s\n", __func__, __LINE__,
971 dev->core.bus_id);
972
Geoff Levand75c86e72007-02-13 17:37:28 -0800973 BUG_ON(vuart_bus_priv.use_count < 1);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800974
975 if (drv->remove)
976 drv->remove(dev);
977 else
978 dev_dbg(&dev->core, "%s:%d: %s no remove method\n", __func__,
979 __LINE__, dev->core.bus_id);
980
Geoff Levand75c86e72007-02-13 17:37:28 -0800981 vuart_bus_priv.devices[dev->priv->port_number] = 0;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800982
Geoff Levand75c86e72007-02-13 17:37:28 -0800983 if (--vuart_bus_priv.use_count == 0) {
984 BUG();
985 free_irq(vuart_bus_priv.virq, &vuart_bus_priv);
986 ps3_free_vuart_irq(vuart_bus_priv.virq);
987 vuart_bus_priv.virq = NO_IRQ;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800988 }
Geoff Levand75c86e72007-02-13 17:37:28 -0800989
990 kfree(dev->priv);
991 dev->priv = NULL;
992
993 up(&vuart_bus_priv.probe_mutex);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800994 return 0;
995}
996
Geert Uytterhoeven5b8e8ee2007-02-12 00:55:15 -0800997static void ps3_vuart_shutdown(struct device *_dev)
998{
999 struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
1000 struct ps3_vuart_port_driver *drv =
1001 to_ps3_vuart_port_driver(_dev->driver);
1002
1003 dev_dbg(&dev->core, "%s:%d: %s\n", __func__, __LINE__,
1004 dev->core.bus_id);
1005
1006 if (drv->shutdown)
1007 drv->shutdown(dev);
1008 else
1009 dev_dbg(&dev->core, "%s:%d: %s no shutdown method\n", __func__,
1010 __LINE__, dev->core.bus_id);
1011}
1012
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001013/**
Geoff Levand75c86e72007-02-13 17:37:28 -08001014 * ps3_vuart_bus - The vuart bus instance.
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001015 *
1016 * The vuart is managed as a bus that port devices connect to.
1017 */
1018
Geoff Levand75c86e72007-02-13 17:37:28 -08001019struct bus_type ps3_vuart_bus = {
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001020 .name = "ps3_vuart",
1021 .match = ps3_vuart_match,
1022 .probe = ps3_vuart_probe,
1023 .remove = ps3_vuart_remove,
Geert Uytterhoeven5b8e8ee2007-02-12 00:55:15 -08001024 .shutdown = ps3_vuart_shutdown,
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001025};
1026
Geoff Levand75c86e72007-02-13 17:37:28 -08001027int __init ps3_vuart_bus_init(void)
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001028{
1029 int result;
1030
1031 pr_debug("%s:%d:\n", __func__, __LINE__);
Geoff Levand75c86e72007-02-13 17:37:28 -08001032
1033 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
1034 return 0;
1035
1036 init_MUTEX(&vuart_bus_priv.probe_mutex);
1037 result = bus_register(&ps3_vuart_bus);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001038 BUG_ON(result);
Geoff Levand75c86e72007-02-13 17:37:28 -08001039
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001040 return result;
1041}
1042
Geoff Levand75c86e72007-02-13 17:37:28 -08001043void __exit ps3_vuart_bus_exit(void)
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001044{
1045 pr_debug("%s:%d:\n", __func__, __LINE__);
Geoff Levand75c86e72007-02-13 17:37:28 -08001046 bus_unregister(&ps3_vuart_bus);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001047}
1048
Geoff Levand75c86e72007-02-13 17:37:28 -08001049core_initcall(ps3_vuart_bus_init);
1050module_exit(ps3_vuart_bus_exit);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001051
1052/**
1053 * ps3_vuart_port_release_device - Remove a vuart port device.
1054 */
1055
1056static void ps3_vuart_port_release_device(struct device *_dev)
1057{
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001058#if defined(DEBUG)
Geoff Levand75c86e72007-02-13 17:37:28 -08001059 struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
1060
1061 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
1062
1063 BUG_ON(dev->priv && "forgot to free");
1064 memset(&dev->core, 0, sizeof(dev->core));
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001065#endif
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001066}
1067
1068/**
1069 * ps3_vuart_port_device_register - Add a vuart port device.
1070 */
1071
1072int ps3_vuart_port_device_register(struct ps3_vuart_port_device *dev)
1073{
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001074 static unsigned int dev_count = 1;
1075
Geoff Levand75c86e72007-02-13 17:37:28 -08001076 BUG_ON(dev->priv && "forgot to free");
1077
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001078 dev->core.parent = NULL;
Geoff Levand75c86e72007-02-13 17:37:28 -08001079 dev->core.bus = &ps3_vuart_bus;
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001080 dev->core.release = ps3_vuart_port_release_device;
1081
1082 snprintf(dev->core.bus_id, sizeof(dev->core.bus_id), "vuart_%02x",
1083 dev_count++);
1084
1085 dev_dbg(&dev->core, "%s:%d register\n", __func__, __LINE__);
1086
Geoff Levand75c86e72007-02-13 17:37:28 -08001087 return device_register(&dev->core);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001088}
1089
1090EXPORT_SYMBOL_GPL(ps3_vuart_port_device_register);
1091
1092/**
1093 * ps3_vuart_port_driver_register - Add a vuart port device driver.
1094 */
1095
1096int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver *drv)
1097{
1098 int result;
1099
1100 pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.name);
Geoff Levand75c86e72007-02-13 17:37:28 -08001101 drv->core.bus = &ps3_vuart_bus;
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001102 result = driver_register(&drv->core);
1103 return result;
1104}
1105
1106EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register);
1107
1108/**
1109 * ps3_vuart_port_driver_unregister - Remove a vuart port device driver.
1110 */
1111
1112void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver *drv)
1113{
Geoff Levand75c86e72007-02-13 17:37:28 -08001114 pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.name);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001115 driver_unregister(&drv->core);
1116}
1117
1118EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister);