Commit | Line | Data |
---|---|---|
74e95d5d GL |
1 | /* |
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> | |
ea1547d3 | 24 | #include <linux/workqueue.h> |
1977f032 | 25 | #include <linux/bitops.h> |
74e95d5d GL |
26 | #include <asm/ps3.h> |
27 | ||
75c86e74 | 28 | #include <asm/firmware.h> |
74e95d5d | 29 | #include <asm/lv1call.h> |
74e95d5d GL |
30 | |
31 | #include "vuart.h" | |
32 | ||
33 | MODULE_AUTHOR("Sony Corporation"); | |
34 | MODULE_LICENSE("GPL v2"); | |
75c86e74 | 35 | MODULE_DESCRIPTION("PS3 vuart"); |
74e95d5d GL |
36 | |
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 | ||
48 | enum {PORT_COUNT = 3,}; | |
49 | ||
50 | enum 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 | ||
61 | enum vuart_interrupt_bit { | |
62 | INTERRUPT_BIT_TX = 0, | |
63 | INTERRUPT_BIT_RX = 1, | |
64 | INTERRUPT_BIT_DISCONNECT = 2, | |
65 | }; | |
66 | ||
67 | enum vuart_interrupt_mask { | |
68 | INTERRUPT_MASK_TX = 1, | |
69 | INTERRUPT_MASK_RX = 2, | |
70 | INTERRUPT_MASK_DISCONNECT = 4, | |
71 | }; | |
72 | ||
7626e78d GL |
73 | /** |
74 | * struct ps3_vuart_port_priv - private vuart device data. | |
75 | */ | |
76 | ||
77 | struct ps3_vuart_port_priv { | |
78 | u64 interrupt_mask; | |
79 | ||
80 | struct { | |
81 | spinlock_t lock; | |
82 | struct list_head head; | |
83 | } tx_list; | |
84 | struct { | |
85 | struct ps3_vuart_work work; | |
86 | unsigned long bytes_held; | |
87 | spinlock_t lock; | |
88 | struct list_head head; | |
89 | } rx_list; | |
90 | struct ps3_vuart_stats stats; | |
91 | }; | |
92 | ||
93 | static struct ps3_vuart_port_priv *to_port_priv( | |
94 | struct ps3_system_bus_device *dev) | |
95 | { | |
96 | BUG_ON(!dev); | |
97 | BUG_ON(!dev->driver_priv); | |
98 | return (struct ps3_vuart_port_priv *)dev->driver_priv; | |
99 | } | |
100 | ||
74e95d5d GL |
101 | /** |
102 | * struct ports_bmp - bitmap indicating ports needing service. | |
103 | * | |
104 | * A 256 bit read only bitmap indicating ports needing service. Do not write | |
105 | * to these bits. Must not cross a page boundary. | |
106 | */ | |
107 | ||
108 | struct ports_bmp { | |
109 | u64 status; | |
110 | u64 unused[3]; | |
d0e5c218 | 111 | } __attribute__((aligned(32))); |
74e95d5d | 112 | |
74e95d5d | 113 | #define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__) |
848cfdc5 | 114 | static void __maybe_unused _dump_ports_bmp( |
d0e5c218 | 115 | const struct ports_bmp *bmp, const char *func, int line) |
74e95d5d | 116 | { |
a9dad6e5 | 117 | pr_debug("%s:%d: ports_bmp: %016llxh\n", func, line, bmp->status); |
74e95d5d GL |
118 | } |
119 | ||
74e95d5d | 120 | #define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__) |
848cfdc5 | 121 | static void __maybe_unused _dump_port_params(unsigned int port_number, |
d0e5c218 | 122 | const char *func, int line) |
74e95d5d GL |
123 | { |
124 | #if defined(DEBUG) | |
125 | static const char *strings[] = { | |
126 | "tx_trigger ", | |
127 | "rx_trigger ", | |
128 | "interrupt_mask ", | |
129 | "rx_buf_size ", | |
130 | "rx_bytes ", | |
131 | "tx_buf_size ", | |
132 | "tx_bytes ", | |
133 | "interrupt_status", | |
134 | }; | |
135 | int result; | |
136 | unsigned int i; | |
137 | u64 value; | |
138 | ||
139 | for (i = 0; i < ARRAY_SIZE(strings); i++) { | |
140 | result = lv1_get_virtual_uart_param(port_number, i, &value); | |
141 | ||
142 | if (result) { | |
143 | pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line, | |
144 | port_number, strings[i], ps3_result(result)); | |
145 | continue; | |
146 | } | |
147 | pr_debug("%s:%d: port_%u: %s = %lxh\n", | |
148 | func, line, port_number, strings[i], value); | |
149 | } | |
150 | #endif | |
151 | } | |
152 | ||
153 | struct vuart_triggers { | |
154 | unsigned long rx; | |
155 | unsigned long tx; | |
156 | }; | |
157 | ||
7626e78d | 158 | int ps3_vuart_get_triggers(struct ps3_system_bus_device *dev, |
74e95d5d GL |
159 | struct vuart_triggers *trig) |
160 | { | |
161 | int result; | |
b17b3df1 SR |
162 | u64 size; |
163 | u64 val; | |
164 | u64 tx; | |
74e95d5d | 165 | |
7626e78d | 166 | result = lv1_get_virtual_uart_param(dev->port_number, |
b17b3df1 SR |
167 | PARAM_TX_TRIGGER, &tx); |
168 | trig->tx = tx; | |
74e95d5d GL |
169 | |
170 | if (result) { | |
171 | dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n", | |
172 | __func__, __LINE__, ps3_result(result)); | |
173 | return result; | |
174 | } | |
175 | ||
7626e78d | 176 | result = lv1_get_virtual_uart_param(dev->port_number, |
74e95d5d GL |
177 | PARAM_RX_BUF_SIZE, &size); |
178 | ||
179 | if (result) { | |
180 | dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n", | |
181 | __func__, __LINE__, ps3_result(result)); | |
182 | return result; | |
183 | } | |
184 | ||
7626e78d | 185 | result = lv1_get_virtual_uart_param(dev->port_number, |
74e95d5d GL |
186 | PARAM_RX_TRIGGER, &val); |
187 | ||
188 | if (result) { | |
189 | dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n", | |
190 | __func__, __LINE__, ps3_result(result)); | |
191 | return result; | |
192 | } | |
193 | ||
194 | trig->rx = size - val; | |
195 | ||
196 | dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__, | |
197 | trig->tx, trig->rx); | |
198 | ||
199 | return result; | |
200 | } | |
201 | ||
7626e78d | 202 | int ps3_vuart_set_triggers(struct ps3_system_bus_device *dev, unsigned int tx, |
74e95d5d GL |
203 | unsigned int rx) |
204 | { | |
205 | int result; | |
b17b3df1 | 206 | u64 size; |
74e95d5d | 207 | |
7626e78d | 208 | result = lv1_set_virtual_uart_param(dev->port_number, |
74e95d5d GL |
209 | PARAM_TX_TRIGGER, tx); |
210 | ||
211 | if (result) { | |
212 | dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n", | |
213 | __func__, __LINE__, ps3_result(result)); | |
214 | return result; | |
215 | } | |
216 | ||
7626e78d | 217 | result = lv1_get_virtual_uart_param(dev->port_number, |
74e95d5d GL |
218 | PARAM_RX_BUF_SIZE, &size); |
219 | ||
220 | if (result) { | |
221 | dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n", | |
222 | __func__, __LINE__, ps3_result(result)); | |
223 | return result; | |
224 | } | |
225 | ||
7626e78d | 226 | result = lv1_set_virtual_uart_param(dev->port_number, |
74e95d5d GL |
227 | PARAM_RX_TRIGGER, size - rx); |
228 | ||
229 | if (result) { | |
230 | dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n", | |
231 | __func__, __LINE__, ps3_result(result)); | |
232 | return result; | |
233 | } | |
234 | ||
235 | dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__, | |
236 | tx, rx); | |
237 | ||
238 | return result; | |
239 | } | |
240 | ||
7626e78d | 241 | static int ps3_vuart_get_rx_bytes_waiting(struct ps3_system_bus_device *dev, |
75c86e74 | 242 | u64 *bytes_waiting) |
74e95d5d | 243 | { |
7626e78d GL |
244 | int result; |
245 | ||
246 | result = lv1_get_virtual_uart_param(dev->port_number, | |
74e95d5d GL |
247 | PARAM_RX_BYTES, bytes_waiting); |
248 | ||
249 | if (result) | |
250 | dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n", | |
251 | __func__, __LINE__, ps3_result(result)); | |
252 | ||
a9dad6e5 | 253 | dev_dbg(&dev->core, "%s:%d: %llxh\n", __func__, __LINE__, |
74e95d5d GL |
254 | *bytes_waiting); |
255 | return result; | |
256 | } | |
257 | ||
7626e78d GL |
258 | /** |
259 | * ps3_vuart_set_interrupt_mask - Enable/disable the port interrupt sources. | |
260 | * @dev: The struct ps3_system_bus_device instance. | |
261 | * @bmp: Logical OR of enum vuart_interrupt_mask values. A zero bit disables. | |
262 | */ | |
263 | ||
264 | static int ps3_vuart_set_interrupt_mask(struct ps3_system_bus_device *dev, | |
74e95d5d GL |
265 | unsigned long mask) |
266 | { | |
267 | int result; | |
7626e78d | 268 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
74e95d5d GL |
269 | |
270 | dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, mask); | |
271 | ||
7626e78d | 272 | priv->interrupt_mask = mask; |
74e95d5d | 273 | |
7626e78d GL |
274 | result = lv1_set_virtual_uart_param(dev->port_number, |
275 | PARAM_INTERRUPT_MASK, priv->interrupt_mask); | |
74e95d5d GL |
276 | |
277 | if (result) | |
278 | dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n", | |
279 | __func__, __LINE__, ps3_result(result)); | |
280 | ||
281 | return result; | |
282 | } | |
283 | ||
7626e78d | 284 | static int ps3_vuart_get_interrupt_status(struct ps3_system_bus_device *dev, |
74e95d5d GL |
285 | unsigned long *status) |
286 | { | |
7626e78d GL |
287 | int result; |
288 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); | |
75c86e74 | 289 | u64 tmp; |
7626e78d GL |
290 | |
291 | result = lv1_get_virtual_uart_param(dev->port_number, | |
75c86e74 | 292 | PARAM_INTERRUPT_STATUS, &tmp); |
74e95d5d GL |
293 | |
294 | if (result) | |
295 | dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n", | |
296 | __func__, __LINE__, ps3_result(result)); | |
297 | ||
7626e78d | 298 | *status = tmp & priv->interrupt_mask; |
75c86e74 | 299 | |
a9dad6e5 | 300 | dev_dbg(&dev->core, "%s:%d: m %llxh, s %llxh, m&s %lxh\n", |
7626e78d | 301 | __func__, __LINE__, priv->interrupt_mask, tmp, *status); |
74e95d5d GL |
302 | |
303 | return result; | |
304 | } | |
305 | ||
7626e78d | 306 | int ps3_vuart_enable_interrupt_tx(struct ps3_system_bus_device *dev) |
74e95d5d | 307 | { |
7626e78d GL |
308 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
309 | ||
310 | return (priv->interrupt_mask & INTERRUPT_MASK_TX) ? 0 | |
311 | : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask | |
74e95d5d GL |
312 | | INTERRUPT_MASK_TX); |
313 | } | |
314 | ||
7626e78d | 315 | int ps3_vuart_enable_interrupt_rx(struct ps3_system_bus_device *dev) |
74e95d5d | 316 | { |
7626e78d GL |
317 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
318 | ||
319 | return (priv->interrupt_mask & INTERRUPT_MASK_RX) ? 0 | |
320 | : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask | |
74e95d5d GL |
321 | | INTERRUPT_MASK_RX); |
322 | } | |
323 | ||
7626e78d | 324 | int ps3_vuart_enable_interrupt_disconnect(struct ps3_system_bus_device *dev) |
74e95d5d | 325 | { |
7626e78d GL |
326 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
327 | ||
328 | return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) ? 0 | |
329 | : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask | |
74e95d5d GL |
330 | | INTERRUPT_MASK_DISCONNECT); |
331 | } | |
332 | ||
7626e78d | 333 | int ps3_vuart_disable_interrupt_tx(struct ps3_system_bus_device *dev) |
74e95d5d | 334 | { |
7626e78d GL |
335 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
336 | ||
337 | return (priv->interrupt_mask & INTERRUPT_MASK_TX) | |
338 | ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask | |
74e95d5d GL |
339 | & ~INTERRUPT_MASK_TX) : 0; |
340 | } | |
341 | ||
7626e78d | 342 | int ps3_vuart_disable_interrupt_rx(struct ps3_system_bus_device *dev) |
74e95d5d | 343 | { |
7626e78d GL |
344 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
345 | ||
346 | return (priv->interrupt_mask & INTERRUPT_MASK_RX) | |
347 | ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask | |
74e95d5d GL |
348 | & ~INTERRUPT_MASK_RX) : 0; |
349 | } | |
350 | ||
7626e78d | 351 | int ps3_vuart_disable_interrupt_disconnect(struct ps3_system_bus_device *dev) |
74e95d5d | 352 | { |
7626e78d GL |
353 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
354 | ||
355 | return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) | |
356 | ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask | |
74e95d5d GL |
357 | & ~INTERRUPT_MASK_DISCONNECT) : 0; |
358 | } | |
359 | ||
360 | /** | |
361 | * ps3_vuart_raw_write - Low level write helper. | |
7626e78d | 362 | * @dev: The struct ps3_system_bus_device instance. |
74e95d5d GL |
363 | * |
364 | * Do not call ps3_vuart_raw_write directly, use ps3_vuart_write. | |
365 | */ | |
366 | ||
7626e78d | 367 | static int ps3_vuart_raw_write(struct ps3_system_bus_device *dev, |
b17b3df1 | 368 | const void *buf, unsigned int bytes, u64 *bytes_written) |
74e95d5d GL |
369 | { |
370 | int result; | |
7626e78d | 371 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
74e95d5d | 372 | |
7626e78d | 373 | result = lv1_write_virtual_uart(dev->port_number, |
74e95d5d GL |
374 | ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written); |
375 | ||
376 | if (result) { | |
377 | dev_dbg(&dev->core, "%s:%d: lv1_write_virtual_uart failed: " | |
378 | "%s\n", __func__, __LINE__, ps3_result(result)); | |
379 | return result; | |
380 | } | |
381 | ||
7626e78d | 382 | priv->stats.bytes_written += *bytes_written; |
74e95d5d | 383 | |
b17b3df1 | 384 | dev_dbg(&dev->core, "%s:%d: wrote %llxh/%xh=>%lxh\n", __func__, __LINE__, |
7626e78d | 385 | *bytes_written, bytes, priv->stats.bytes_written); |
74e95d5d GL |
386 | |
387 | return result; | |
388 | } | |
389 | ||
390 | /** | |
391 | * ps3_vuart_raw_read - Low level read helper. | |
7626e78d | 392 | * @dev: The struct ps3_system_bus_device instance. |
74e95d5d GL |
393 | * |
394 | * Do not call ps3_vuart_raw_read directly, use ps3_vuart_read. | |
395 | */ | |
396 | ||
7626e78d | 397 | static int ps3_vuart_raw_read(struct ps3_system_bus_device *dev, void *buf, |
b17b3df1 | 398 | unsigned int bytes, u64 *bytes_read) |
74e95d5d GL |
399 | { |
400 | int result; | |
7626e78d | 401 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
74e95d5d GL |
402 | |
403 | dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes); | |
404 | ||
7626e78d | 405 | result = lv1_read_virtual_uart(dev->port_number, |
74e95d5d GL |
406 | ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_read); |
407 | ||
408 | if (result) { | |
409 | dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n", | |
410 | __func__, __LINE__, ps3_result(result)); | |
411 | return result; | |
412 | } | |
413 | ||
7626e78d | 414 | priv->stats.bytes_read += *bytes_read; |
74e95d5d | 415 | |
b17b3df1 | 416 | dev_dbg(&dev->core, "%s:%d: read %llxh/%xh=>%lxh\n", __func__, __LINE__, |
7626e78d | 417 | *bytes_read, bytes, priv->stats.bytes_read); |
74e95d5d GL |
418 | |
419 | return result; | |
420 | } | |
421 | ||
75c86e74 GL |
422 | /** |
423 | * ps3_vuart_clear_rx_bytes - Discard bytes received. | |
7626e78d | 424 | * @dev: The struct ps3_system_bus_device instance. |
75c86e74 GL |
425 | * @bytes: Max byte count to discard, zero = all pending. |
426 | * | |
427 | * Used to clear pending rx interrupt source. Will not block. | |
428 | */ | |
429 | ||
7626e78d | 430 | void ps3_vuart_clear_rx_bytes(struct ps3_system_bus_device *dev, |
75c86e74 GL |
431 | unsigned int bytes) |
432 | { | |
433 | int result; | |
7626e78d | 434 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
75c86e74 | 435 | u64 bytes_waiting; |
d0e5c218 | 436 | void *tmp; |
75c86e74 GL |
437 | |
438 | result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting); | |
439 | ||
440 | BUG_ON(result); | |
441 | ||
442 | bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting; | |
443 | ||
444 | dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes); | |
445 | ||
446 | if (!bytes) | |
447 | return; | |
448 | ||
449 | /* Add some extra space for recently arrived data. */ | |
450 | ||
451 | bytes += 128; | |
452 | ||
453 | tmp = kmalloc(bytes, GFP_KERNEL); | |
454 | ||
455 | if (!tmp) | |
456 | return; | |
457 | ||
458 | ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting); | |
459 | ||
460 | kfree(tmp); | |
461 | ||
462 | /* Don't include these bytes in the stats. */ | |
463 | ||
7626e78d | 464 | priv->stats.bytes_read -= bytes_waiting; |
75c86e74 | 465 | } |
7626e78d | 466 | EXPORT_SYMBOL_GPL(ps3_vuart_clear_rx_bytes); |
75c86e74 | 467 | |
74e95d5d GL |
468 | /** |
469 | * struct list_buffer - An element for a port device fifo buffer list. | |
470 | */ | |
471 | ||
472 | struct list_buffer { | |
473 | struct list_head link; | |
474 | const unsigned char *head; | |
475 | const unsigned char *tail; | |
476 | unsigned long dbg_number; | |
477 | unsigned char data[]; | |
478 | }; | |
479 | ||
480 | /** | |
481 | * ps3_vuart_write - the entry point for writing data to a port | |
7626e78d | 482 | * @dev: The struct ps3_system_bus_device instance. |
74e95d5d GL |
483 | * |
484 | * If the port is idle on entry as much of the incoming data is written to | |
485 | * the port as the port will accept. Otherwise a list buffer is created | |
486 | * and any remaning incoming data is copied to that buffer. The buffer is | |
487 | * then enqueued for transmision via the transmit interrupt. | |
488 | */ | |
489 | ||
7626e78d | 490 | int ps3_vuart_write(struct ps3_system_bus_device *dev, const void *buf, |
74e95d5d GL |
491 | unsigned int bytes) |
492 | { | |
493 | static unsigned long dbg_number; | |
494 | int result; | |
7626e78d | 495 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
74e95d5d GL |
496 | unsigned long flags; |
497 | struct list_buffer *lb; | |
498 | ||
499 | dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__, | |
500 | bytes, bytes); | |
501 | ||
7626e78d | 502 | spin_lock_irqsave(&priv->tx_list.lock, flags); |
74e95d5d | 503 | |
7626e78d | 504 | if (list_empty(&priv->tx_list.head)) { |
b17b3df1 | 505 | u64 bytes_written; |
74e95d5d GL |
506 | |
507 | result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written); | |
508 | ||
7626e78d | 509 | spin_unlock_irqrestore(&priv->tx_list.lock, flags); |
74e95d5d GL |
510 | |
511 | if (result) { | |
512 | dev_dbg(&dev->core, | |
513 | "%s:%d: ps3_vuart_raw_write failed\n", | |
514 | __func__, __LINE__); | |
515 | return result; | |
516 | } | |
517 | ||
518 | if (bytes_written == bytes) { | |
519 | dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n", | |
520 | __func__, __LINE__, bytes); | |
521 | return 0; | |
522 | } | |
523 | ||
524 | bytes -= bytes_written; | |
525 | buf += bytes_written; | |
526 | } else | |
7626e78d | 527 | spin_unlock_irqrestore(&priv->tx_list.lock, flags); |
74e95d5d GL |
528 | |
529 | lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL); | |
530 | ||
d0e5c218 | 531 | if (!lb) |
74e95d5d | 532 | return -ENOMEM; |
74e95d5d GL |
533 | |
534 | memcpy(lb->data, buf, bytes); | |
535 | lb->head = lb->data; | |
536 | lb->tail = lb->data + bytes; | |
537 | lb->dbg_number = ++dbg_number; | |
538 | ||
7626e78d GL |
539 | spin_lock_irqsave(&priv->tx_list.lock, flags); |
540 | list_add_tail(&lb->link, &priv->tx_list.head); | |
74e95d5d | 541 | ps3_vuart_enable_interrupt_tx(dev); |
7626e78d | 542 | spin_unlock_irqrestore(&priv->tx_list.lock, flags); |
74e95d5d GL |
543 | |
544 | dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %xh bytes\n", | |
545 | __func__, __LINE__, lb->dbg_number, bytes); | |
546 | ||
547 | return 0; | |
548 | } | |
7626e78d GL |
549 | EXPORT_SYMBOL_GPL(ps3_vuart_write); |
550 | ||
551 | /** | |
552 | * ps3_vuart_queue_rx_bytes - Queue waiting bytes into the buffer list. | |
553 | * @dev: The struct ps3_system_bus_device instance. | |
554 | * @bytes_queued: Number of bytes queued to the buffer list. | |
555 | * | |
556 | * Must be called with priv->rx_list.lock held. | |
557 | */ | |
558 | ||
559 | static int ps3_vuart_queue_rx_bytes(struct ps3_system_bus_device *dev, | |
560 | u64 *bytes_queued) | |
561 | { | |
562 | static unsigned long dbg_number; | |
563 | int result; | |
564 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); | |
565 | struct list_buffer *lb; | |
566 | u64 bytes; | |
567 | ||
568 | *bytes_queued = 0; | |
569 | ||
570 | result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes); | |
571 | BUG_ON(result); | |
572 | ||
573 | if (result) | |
574 | return -EIO; | |
575 | ||
576 | if (!bytes) | |
577 | return 0; | |
578 | ||
579 | /* Add some extra space for recently arrived data. */ | |
580 | ||
581 | bytes += 128; | |
582 | ||
583 | lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC); | |
584 | ||
585 | if (!lb) | |
586 | return -ENOMEM; | |
587 | ||
588 | ps3_vuart_raw_read(dev, lb->data, bytes, &bytes); | |
589 | ||
590 | lb->head = lb->data; | |
591 | lb->tail = lb->data + bytes; | |
592 | lb->dbg_number = ++dbg_number; | |
593 | ||
594 | list_add_tail(&lb->link, &priv->rx_list.head); | |
595 | priv->rx_list.bytes_held += bytes; | |
596 | ||
a9dad6e5 | 597 | dev_dbg(&dev->core, "%s:%d: buf_%lu: queued %llxh bytes\n", |
7626e78d GL |
598 | __func__, __LINE__, lb->dbg_number, bytes); |
599 | ||
600 | *bytes_queued = bytes; | |
601 | ||
602 | return 0; | |
603 | } | |
74e95d5d GL |
604 | |
605 | /** | |
7626e78d | 606 | * ps3_vuart_read - The entry point for reading data from a port. |
74e95d5d | 607 | * |
7626e78d GL |
608 | * Queue data waiting at the port, and if enough bytes to satisfy the request |
609 | * are held in the buffer list those bytes are dequeued and copied to the | |
610 | * caller's buffer. Emptied list buffers are retiered. If the request cannot | |
611 | * be statified by bytes held in the list buffers -EAGAIN is returned. | |
74e95d5d GL |
612 | */ |
613 | ||
7626e78d | 614 | int ps3_vuart_read(struct ps3_system_bus_device *dev, void *buf, |
74e95d5d GL |
615 | unsigned int bytes) |
616 | { | |
7626e78d GL |
617 | int result; |
618 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); | |
74e95d5d GL |
619 | unsigned long flags; |
620 | struct list_buffer *lb, *n; | |
621 | unsigned long bytes_read; | |
622 | ||
623 | dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__, | |
624 | bytes, bytes); | |
625 | ||
7626e78d | 626 | spin_lock_irqsave(&priv->rx_list.lock, flags); |
74e95d5d | 627 | |
7626e78d GL |
628 | /* Queue rx bytes here for polled reads. */ |
629 | ||
630 | while (priv->rx_list.bytes_held < bytes) { | |
631 | u64 tmp; | |
632 | ||
633 | result = ps3_vuart_queue_rx_bytes(dev, &tmp); | |
634 | if (result || !tmp) { | |
635 | dev_dbg(&dev->core, "%s:%d: starved for %lxh bytes\n", | |
636 | __func__, __LINE__, | |
637 | bytes - priv->rx_list.bytes_held); | |
638 | spin_unlock_irqrestore(&priv->rx_list.lock, flags); | |
639 | return -EAGAIN; | |
640 | } | |
74e95d5d GL |
641 | } |
642 | ||
7626e78d | 643 | list_for_each_entry_safe(lb, n, &priv->rx_list.head, link) { |
74e95d5d GL |
644 | bytes_read = min((unsigned int)(lb->tail - lb->head), bytes); |
645 | ||
646 | memcpy(buf, lb->head, bytes_read); | |
647 | buf += bytes_read; | |
648 | bytes -= bytes_read; | |
7626e78d | 649 | priv->rx_list.bytes_held -= bytes_read; |
74e95d5d GL |
650 | |
651 | if (bytes_read < lb->tail - lb->head) { | |
652 | lb->head += bytes_read; | |
75c86e74 GL |
653 | dev_dbg(&dev->core, "%s:%d: buf_%lu: dequeued %lxh " |
654 | "bytes\n", __func__, __LINE__, lb->dbg_number, | |
655 | bytes_read); | |
7626e78d | 656 | spin_unlock_irqrestore(&priv->rx_list.lock, flags); |
74e95d5d GL |
657 | return 0; |
658 | } | |
659 | ||
75c86e74 GL |
660 | dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh " |
661 | "bytes\n", __func__, __LINE__, lb->dbg_number, | |
662 | bytes_read); | |
74e95d5d GL |
663 | |
664 | list_del(&lb->link); | |
665 | kfree(lb); | |
666 | } | |
74e95d5d | 667 | |
7626e78d | 668 | spin_unlock_irqrestore(&priv->rx_list.lock, flags); |
74e95d5d GL |
669 | return 0; |
670 | } | |
7626e78d | 671 | EXPORT_SYMBOL_GPL(ps3_vuart_read); |
74e95d5d | 672 | |
7626e78d GL |
673 | /** |
674 | * ps3_vuart_work - Asynchronous read handler. | |
675 | */ | |
676 | ||
677 | static void ps3_vuart_work(struct work_struct *work) | |
678 | { | |
679 | struct ps3_system_bus_device *dev = | |
680 | ps3_vuart_work_to_system_bus_dev(work); | |
681 | struct ps3_vuart_port_driver *drv = | |
682 | ps3_system_bus_dev_to_vuart_drv(dev); | |
683 | ||
684 | BUG_ON(!drv); | |
685 | drv->work(dev); | |
686 | } | |
687 | ||
688 | int ps3_vuart_read_async(struct ps3_system_bus_device *dev, unsigned int bytes) | |
ea1547d3 | 689 | { |
7626e78d | 690 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
ea1547d3 GL |
691 | unsigned long flags; |
692 | ||
7626e78d | 693 | if (priv->rx_list.work.trigger) { |
ea1547d3 GL |
694 | dev_dbg(&dev->core, "%s:%d: warning, multiple calls\n", |
695 | __func__, __LINE__); | |
696 | return -EAGAIN; | |
697 | } | |
698 | ||
699 | BUG_ON(!bytes); | |
700 | ||
7626e78d | 701 | PREPARE_WORK(&priv->rx_list.work.work, ps3_vuart_work); |
ea1547d3 | 702 | |
7626e78d GL |
703 | spin_lock_irqsave(&priv->rx_list.lock, flags); |
704 | if (priv->rx_list.bytes_held >= bytes) { | |
ea1547d3 GL |
705 | dev_dbg(&dev->core, "%s:%d: schedule_work %xh bytes\n", |
706 | __func__, __LINE__, bytes); | |
7626e78d GL |
707 | schedule_work(&priv->rx_list.work.work); |
708 | spin_unlock_irqrestore(&priv->rx_list.lock, flags); | |
ea1547d3 GL |
709 | return 0; |
710 | } | |
711 | ||
7626e78d GL |
712 | priv->rx_list.work.trigger = bytes; |
713 | spin_unlock_irqrestore(&priv->rx_list.lock, flags); | |
ea1547d3 GL |
714 | |
715 | dev_dbg(&dev->core, "%s:%d: waiting for %u(%xh) bytes\n", __func__, | |
716 | __LINE__, bytes, bytes); | |
717 | ||
718 | return 0; | |
719 | } | |
7626e78d | 720 | EXPORT_SYMBOL_GPL(ps3_vuart_read_async); |
ea1547d3 | 721 | |
7626e78d | 722 | void ps3_vuart_cancel_async(struct ps3_system_bus_device *dev) |
ea1547d3 | 723 | { |
7626e78d | 724 | to_port_priv(dev)->rx_list.work.trigger = 0; |
ea1547d3 | 725 | } |
7626e78d | 726 | EXPORT_SYMBOL_GPL(ps3_vuart_cancel_async); |
ea1547d3 | 727 | |
74e95d5d GL |
728 | /** |
729 | * ps3_vuart_handle_interrupt_tx - third stage transmit interrupt handler | |
730 | * | |
731 | * Services the transmit interrupt for the port. Writes as much data from the | |
732 | * buffer list as the port will accept. Retires any emptied list buffers and | |
733 | * adjusts the final list buffer state for a partial write. | |
734 | */ | |
735 | ||
7626e78d | 736 | static int ps3_vuart_handle_interrupt_tx(struct ps3_system_bus_device *dev) |
74e95d5d GL |
737 | { |
738 | int result = 0; | |
7626e78d | 739 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
74e95d5d GL |
740 | unsigned long flags; |
741 | struct list_buffer *lb, *n; | |
742 | unsigned long bytes_total = 0; | |
743 | ||
744 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); | |
745 | ||
7626e78d | 746 | spin_lock_irqsave(&priv->tx_list.lock, flags); |
74e95d5d | 747 | |
7626e78d | 748 | list_for_each_entry_safe(lb, n, &priv->tx_list.head, link) { |
74e95d5d | 749 | |
b17b3df1 | 750 | u64 bytes_written; |
74e95d5d GL |
751 | |
752 | result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head, | |
753 | &bytes_written); | |
754 | ||
755 | if (result) { | |
756 | dev_dbg(&dev->core, | |
757 | "%s:%d: ps3_vuart_raw_write failed\n", | |
758 | __func__, __LINE__); | |
759 | break; | |
760 | } | |
761 | ||
762 | bytes_total += bytes_written; | |
763 | ||
764 | if (bytes_written < lb->tail - lb->head) { | |
765 | lb->head += bytes_written; | |
766 | dev_dbg(&dev->core, | |
b17b3df1 | 767 | "%s:%d cleared buf_%lu, %llxh bytes\n", |
74e95d5d GL |
768 | __func__, __LINE__, lb->dbg_number, |
769 | bytes_written); | |
770 | goto port_full; | |
771 | } | |
772 | ||
773 | dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__, | |
774 | lb->dbg_number); | |
775 | ||
776 | list_del(&lb->link); | |
777 | kfree(lb); | |
778 | } | |
779 | ||
780 | ps3_vuart_disable_interrupt_tx(dev); | |
781 | port_full: | |
7626e78d | 782 | spin_unlock_irqrestore(&priv->tx_list.lock, flags); |
74e95d5d GL |
783 | dev_dbg(&dev->core, "%s:%d wrote %lxh bytes total\n", |
784 | __func__, __LINE__, bytes_total); | |
785 | return result; | |
786 | } | |
787 | ||
788 | /** | |
789 | * ps3_vuart_handle_interrupt_rx - third stage receive interrupt handler | |
790 | * | |
791 | * Services the receive interrupt for the port. Creates a list buffer and | |
792 | * copies all waiting port data to that buffer and enqueues the buffer in the | |
793 | * buffer list. Buffer list data is dequeued via ps3_vuart_read. | |
794 | */ | |
795 | ||
7626e78d | 796 | static int ps3_vuart_handle_interrupt_rx(struct ps3_system_bus_device *dev) |
74e95d5d | 797 | { |
7626e78d GL |
798 | int result; |
799 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); | |
74e95d5d | 800 | unsigned long flags; |
7626e78d | 801 | u64 bytes; |
74e95d5d GL |
802 | |
803 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); | |
804 | ||
7626e78d GL |
805 | spin_lock_irqsave(&priv->rx_list.lock, flags); |
806 | result = ps3_vuart_queue_rx_bytes(dev, &bytes); | |
74e95d5d | 807 | |
7626e78d GL |
808 | if (result) { |
809 | spin_unlock_irqrestore(&priv->rx_list.lock, flags); | |
810 | return result; | |
811 | } | |
74e95d5d | 812 | |
7626e78d GL |
813 | if (priv->rx_list.work.trigger && priv->rx_list.bytes_held |
814 | >= priv->rx_list.work.trigger) { | |
ea1547d3 | 815 | dev_dbg(&dev->core, "%s:%d: schedule_work %lxh bytes\n", |
7626e78d GL |
816 | __func__, __LINE__, priv->rx_list.work.trigger); |
817 | priv->rx_list.work.trigger = 0; | |
818 | schedule_work(&priv->rx_list.work.work); | |
ea1547d3 | 819 | } |
7626e78d GL |
820 | |
821 | spin_unlock_irqrestore(&priv->rx_list.lock, flags); | |
822 | return result; | |
74e95d5d GL |
823 | } |
824 | ||
825 | static int ps3_vuart_handle_interrupt_disconnect( | |
7626e78d | 826 | struct ps3_system_bus_device *dev) |
74e95d5d GL |
827 | { |
828 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); | |
829 | BUG_ON("no support"); | |
830 | return -1; | |
831 | } | |
832 | ||
833 | /** | |
834 | * ps3_vuart_handle_port_interrupt - second stage interrupt handler | |
835 | * | |
836 | * Services any pending interrupt types for the port. Passes control to the | |
837 | * third stage type specific interrupt handler. Returns control to the first | |
838 | * stage handler after one iteration. | |
839 | */ | |
840 | ||
7626e78d | 841 | static int ps3_vuart_handle_port_interrupt(struct ps3_system_bus_device *dev) |
74e95d5d GL |
842 | { |
843 | int result; | |
7626e78d | 844 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
74e95d5d GL |
845 | unsigned long status; |
846 | ||
75c86e74 | 847 | result = ps3_vuart_get_interrupt_status(dev, &status); |
74e95d5d GL |
848 | |
849 | if (result) | |
850 | return result; | |
851 | ||
852 | dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__, | |
853 | status); | |
854 | ||
855 | if (status & INTERRUPT_MASK_DISCONNECT) { | |
7626e78d | 856 | priv->stats.disconnect_interrupts++; |
74e95d5d GL |
857 | result = ps3_vuart_handle_interrupt_disconnect(dev); |
858 | if (result) | |
859 | ps3_vuart_disable_interrupt_disconnect(dev); | |
860 | } | |
861 | ||
862 | if (status & INTERRUPT_MASK_TX) { | |
7626e78d | 863 | priv->stats.tx_interrupts++; |
74e95d5d GL |
864 | result = ps3_vuart_handle_interrupt_tx(dev); |
865 | if (result) | |
866 | ps3_vuart_disable_interrupt_tx(dev); | |
867 | } | |
868 | ||
869 | if (status & INTERRUPT_MASK_RX) { | |
7626e78d | 870 | priv->stats.rx_interrupts++; |
74e95d5d GL |
871 | result = ps3_vuart_handle_interrupt_rx(dev); |
872 | if (result) | |
873 | ps3_vuart_disable_interrupt_rx(dev); | |
874 | } | |
875 | ||
876 | return 0; | |
877 | } | |
878 | ||
75c86e74 | 879 | struct vuart_bus_priv { |
7626e78d | 880 | struct ports_bmp *bmp; |
74e95d5d | 881 | unsigned int virq; |
c4e6752d | 882 | struct mutex probe_mutex; |
75c86e74 | 883 | int use_count; |
7626e78d | 884 | struct ps3_system_bus_device *devices[PORT_COUNT]; |
75c86e74 | 885 | } static vuart_bus_priv; |
74e95d5d GL |
886 | |
887 | /** | |
888 | * ps3_vuart_irq_handler - first stage interrupt handler | |
889 | * | |
890 | * Loops finding any interrupting port and its associated instance data. | |
891 | * Passes control to the second stage port specific interrupt handler. Loops | |
892 | * until all outstanding interrupts are serviced. | |
893 | */ | |
894 | ||
895 | static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private) | |
896 | { | |
7626e78d | 897 | struct vuart_bus_priv *bus_priv = _private; |
74e95d5d | 898 | |
7626e78d | 899 | BUG_ON(!bus_priv); |
74e95d5d GL |
900 | |
901 | while (1) { | |
902 | unsigned int port; | |
903 | ||
7626e78d | 904 | dump_ports_bmp(bus_priv->bmp); |
74e95d5d | 905 | |
7626e78d | 906 | port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp->status); |
74e95d5d GL |
907 | |
908 | if (port == BITS_PER_LONG) | |
909 | break; | |
910 | ||
911 | BUG_ON(port >= PORT_COUNT); | |
75c86e74 | 912 | BUG_ON(!bus_priv->devices[port]); |
74e95d5d | 913 | |
75c86e74 | 914 | ps3_vuart_handle_port_interrupt(bus_priv->devices[port]); |
74e95d5d GL |
915 | } |
916 | ||
917 | return IRQ_HANDLED; | |
918 | } | |
919 | ||
7626e78d | 920 | static int ps3_vuart_bus_interrupt_get(void) |
74e95d5d GL |
921 | { |
922 | int result; | |
74e95d5d | 923 | |
7626e78d GL |
924 | pr_debug(" -> %s:%d\n", __func__, __LINE__); |
925 | ||
926 | vuart_bus_priv.use_count++; | |
927 | ||
928 | BUG_ON(vuart_bus_priv.use_count > 2); | |
929 | ||
d0e5c218 | 930 | if (vuart_bus_priv.use_count != 1) |
7626e78d | 931 | return 0; |
7626e78d GL |
932 | |
933 | BUG_ON(vuart_bus_priv.bmp); | |
934 | ||
935 | vuart_bus_priv.bmp = kzalloc(sizeof(struct ports_bmp), GFP_KERNEL); | |
936 | ||
937 | if (!vuart_bus_priv.bmp) { | |
938 | pr_debug("%s:%d: kzalloc failed.\n", __func__, __LINE__); | |
939 | result = -ENOMEM; | |
940 | goto fail_bmp_malloc; | |
941 | } | |
942 | ||
943 | result = ps3_vuart_irq_setup(PS3_BINDING_CPU_ANY, vuart_bus_priv.bmp, | |
944 | &vuart_bus_priv.virq); | |
945 | ||
946 | if (result) { | |
947 | pr_debug("%s:%d: ps3_vuart_irq_setup failed (%d)\n", | |
948 | __func__, __LINE__, result); | |
949 | result = -EPERM; | |
950 | goto fail_alloc_irq; | |
951 | } | |
952 | ||
953 | result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler, | |
954 | IRQF_DISABLED, "vuart", &vuart_bus_priv); | |
74e95d5d | 955 | |
7626e78d GL |
956 | if (result) { |
957 | pr_debug("%s:%d: request_irq failed (%d)\n", | |
958 | __func__, __LINE__, result); | |
959 | goto fail_request_irq; | |
960 | } | |
74e95d5d | 961 | |
7626e78d | 962 | pr_debug(" <- %s:%d: ok\n", __func__, __LINE__); |
74e95d5d | 963 | return result; |
7626e78d GL |
964 | |
965 | fail_request_irq: | |
966 | ps3_vuart_irq_destroy(vuart_bus_priv.virq); | |
967 | vuart_bus_priv.virq = NO_IRQ; | |
968 | fail_alloc_irq: | |
969 | kfree(vuart_bus_priv.bmp); | |
970 | vuart_bus_priv.bmp = NULL; | |
971 | fail_bmp_malloc: | |
972 | vuart_bus_priv.use_count--; | |
973 | pr_debug(" <- %s:%d: failed\n", __func__, __LINE__); | |
974 | return result; | |
975 | } | |
976 | ||
977 | static int ps3_vuart_bus_interrupt_put(void) | |
978 | { | |
979 | pr_debug(" -> %s:%d\n", __func__, __LINE__); | |
980 | ||
981 | vuart_bus_priv.use_count--; | |
982 | ||
983 | BUG_ON(vuart_bus_priv.use_count < 0); | |
984 | ||
985 | if (vuart_bus_priv.use_count != 0) | |
986 | return 0; | |
987 | ||
988 | free_irq(vuart_bus_priv.virq, &vuart_bus_priv); | |
989 | ||
990 | ps3_vuart_irq_destroy(vuart_bus_priv.virq); | |
991 | vuart_bus_priv.virq = NO_IRQ; | |
992 | ||
993 | kfree(vuart_bus_priv.bmp); | |
994 | vuart_bus_priv.bmp = NULL; | |
995 | ||
996 | pr_debug(" <- %s:%d\n", __func__, __LINE__); | |
997 | return 0; | |
74e95d5d GL |
998 | } |
999 | ||
7626e78d | 1000 | static int ps3_vuart_probe(struct ps3_system_bus_device *dev) |
74e95d5d GL |
1001 | { |
1002 | int result; | |
7626e78d GL |
1003 | struct ps3_vuart_port_driver *drv; |
1004 | struct ps3_vuart_port_priv *priv = NULL; | |
74e95d5d GL |
1005 | |
1006 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); | |
1007 | ||
7626e78d GL |
1008 | drv = ps3_system_bus_dev_to_vuart_drv(dev); |
1009 | ||
1010 | dev_dbg(&dev->core, "%s:%d: (%s)\n", __func__, __LINE__, | |
1011 | drv->core.core.name); | |
1012 | ||
74e95d5d GL |
1013 | BUG_ON(!drv); |
1014 | ||
7626e78d GL |
1015 | if (dev->port_number >= PORT_COUNT) { |
1016 | BUG(); | |
1017 | return -EINVAL; | |
1018 | } | |
75c86e74 | 1019 | |
c4e6752d | 1020 | mutex_lock(&vuart_bus_priv.probe_mutex); |
75c86e74 | 1021 | |
7626e78d | 1022 | result = ps3_vuart_bus_interrupt_get(); |
74e95d5d | 1023 | |
7626e78d GL |
1024 | if (result) |
1025 | goto fail_setup_interrupt; | |
74e95d5d | 1026 | |
7626e78d | 1027 | if (vuart_bus_priv.devices[dev->port_number]) { |
74e95d5d | 1028 | dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__, |
7626e78d | 1029 | __LINE__, dev->port_number); |
74e95d5d | 1030 | result = -EBUSY; |
7626e78d | 1031 | goto fail_busy; |
74e95d5d GL |
1032 | } |
1033 | ||
7626e78d | 1034 | vuart_bus_priv.devices[dev->port_number] = dev; |
75c86e74 | 1035 | |
7626e78d | 1036 | /* Setup dev->driver_priv. */ |
75c86e74 | 1037 | |
7626e78d GL |
1038 | dev->driver_priv = kzalloc(sizeof(struct ps3_vuart_port_priv), |
1039 | GFP_KERNEL); | |
75c86e74 | 1040 | |
7626e78d | 1041 | if (!dev->driver_priv) { |
75c86e74 | 1042 | result = -ENOMEM; |
7626e78d | 1043 | goto fail_dev_malloc; |
75c86e74 GL |
1044 | } |
1045 | ||
7626e78d | 1046 | priv = to_port_priv(dev); |
75c86e74 | 1047 | |
7626e78d GL |
1048 | INIT_LIST_HEAD(&priv->tx_list.head); |
1049 | spin_lock_init(&priv->tx_list.lock); | |
75c86e74 | 1050 | |
7626e78d GL |
1051 | INIT_LIST_HEAD(&priv->rx_list.head); |
1052 | spin_lock_init(&priv->rx_list.lock); | |
ea1547d3 | 1053 | |
7626e78d GL |
1054 | INIT_WORK(&priv->rx_list.work.work, NULL); |
1055 | priv->rx_list.work.trigger = 0; | |
1056 | priv->rx_list.work.dev = dev; | |
74e95d5d | 1057 | |
74e95d5d | 1058 | /* clear stale pending interrupts */ |
75c86e74 GL |
1059 | |
1060 | ps3_vuart_clear_rx_bytes(dev, 0); | |
1061 | ||
1062 | ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX); | |
74e95d5d GL |
1063 | |
1064 | ps3_vuart_set_triggers(dev, 1, 1); | |
1065 | ||
1066 | if (drv->probe) | |
1067 | result = drv->probe(dev); | |
1068 | else { | |
1069 | result = 0; | |
1070 | dev_info(&dev->core, "%s:%d: no probe method\n", __func__, | |
1071 | __LINE__); | |
1072 | } | |
1073 | ||
1074 | if (result) { | |
1075 | dev_dbg(&dev->core, "%s:%d: drv->probe failed\n", | |
1076 | __func__, __LINE__); | |
1077 | goto fail_probe; | |
1078 | } | |
1079 | ||
c4e6752d | 1080 | mutex_unlock(&vuart_bus_priv.probe_mutex); |
75c86e74 | 1081 | |
74e95d5d GL |
1082 | return result; |
1083 | ||
1084 | fail_probe: | |
75c86e74 | 1085 | ps3_vuart_set_interrupt_mask(dev, 0); |
7626e78d GL |
1086 | kfree(dev->driver_priv); |
1087 | dev->driver_priv = NULL; | |
1088 | fail_dev_malloc: | |
1089 | vuart_bus_priv.devices[dev->port_number] = NULL; | |
1090 | fail_busy: | |
1091 | ps3_vuart_bus_interrupt_put(); | |
1092 | fail_setup_interrupt: | |
c4e6752d | 1093 | mutex_unlock(&vuart_bus_priv.probe_mutex); |
7626e78d | 1094 | dev_dbg(&dev->core, "%s:%d: failed\n", __func__, __LINE__); |
74e95d5d GL |
1095 | return result; |
1096 | } | |
1097 | ||
7626e78d GL |
1098 | /** |
1099 | * ps3_vuart_cleanup - common cleanup helper. | |
1100 | * @dev: The struct ps3_system_bus_device instance. | |
1101 | * | |
1102 | * Cleans interrupts and HV resources. Must be called with | |
1103 | * vuart_bus_priv.probe_mutex held. Used by ps3_vuart_remove and | |
1104 | * ps3_vuart_shutdown. After this call, polled reading will still work. | |
1105 | */ | |
1106 | ||
1107 | static int ps3_vuart_cleanup(struct ps3_system_bus_device *dev) | |
74e95d5d | 1108 | { |
7626e78d GL |
1109 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); |
1110 | ||
1111 | ps3_vuart_cancel_async(dev); | |
1112 | ps3_vuart_set_interrupt_mask(dev, 0); | |
1113 | ps3_vuart_bus_interrupt_put(); | |
1114 | return 0; | |
1115 | } | |
1116 | ||
1117 | /** | |
1118 | * ps3_vuart_remove - Completely clean the device instance. | |
1119 | * @dev: The struct ps3_system_bus_device instance. | |
1120 | * | |
1121 | * Cleans all memory, interrupts and HV resources. After this call the | |
1122 | * device can no longer be used. | |
1123 | */ | |
1124 | ||
1125 | static int ps3_vuart_remove(struct ps3_system_bus_device *dev) | |
1126 | { | |
1127 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); | |
1128 | struct ps3_vuart_port_driver *drv; | |
1129 | ||
1130 | BUG_ON(!dev); | |
74e95d5d | 1131 | |
c4e6752d | 1132 | mutex_lock(&vuart_bus_priv.probe_mutex); |
75c86e74 | 1133 | |
7626e78d GL |
1134 | dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__, |
1135 | dev->match_id); | |
74e95d5d | 1136 | |
7626e78d GL |
1137 | if (!dev->core.driver) { |
1138 | dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__, | |
1139 | __LINE__); | |
c4e6752d | 1140 | mutex_unlock(&vuart_bus_priv.probe_mutex); |
7626e78d GL |
1141 | return 0; |
1142 | } | |
74e95d5d | 1143 | |
7626e78d | 1144 | drv = ps3_system_bus_dev_to_vuart_drv(dev); |
74e95d5d | 1145 | |
7626e78d | 1146 | BUG_ON(!drv); |
74e95d5d | 1147 | |
7626e78d GL |
1148 | if (drv->remove) { |
1149 | drv->remove(dev); | |
1150 | } else { | |
1151 | dev_dbg(&dev->core, "%s:%d: no remove method\n", __func__, | |
1152 | __LINE__); | |
75c86e74 | 1153 | BUG(); |
74e95d5d | 1154 | } |
75c86e74 | 1155 | |
7626e78d GL |
1156 | ps3_vuart_cleanup(dev); |
1157 | ||
1158 | vuart_bus_priv.devices[dev->port_number] = NULL; | |
1159 | kfree(priv); | |
1160 | priv = NULL; | |
75c86e74 | 1161 | |
7626e78d | 1162 | dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__); |
c4e6752d | 1163 | mutex_unlock(&vuart_bus_priv.probe_mutex); |
74e95d5d GL |
1164 | return 0; |
1165 | } | |
1166 | ||
1167 | /** | |
7626e78d GL |
1168 | * ps3_vuart_shutdown - Cleans interrupts and HV resources. |
1169 | * @dev: The struct ps3_system_bus_device instance. | |
74e95d5d | 1170 | * |
7626e78d GL |
1171 | * Cleans interrupts and HV resources. After this call the |
1172 | * device can still be used in polling mode. This behavior required | |
1173 | * by sys-manager to be able to complete the device power operation | |
1174 | * sequence. | |
74e95d5d GL |
1175 | */ |
1176 | ||
7626e78d | 1177 | static int ps3_vuart_shutdown(struct ps3_system_bus_device *dev) |
74e95d5d | 1178 | { |
7626e78d | 1179 | struct ps3_vuart_port_driver *drv; |
74e95d5d | 1180 | |
7626e78d | 1181 | BUG_ON(!dev); |
75c86e74 | 1182 | |
c4e6752d | 1183 | mutex_lock(&vuart_bus_priv.probe_mutex); |
75c86e74 | 1184 | |
7626e78d GL |
1185 | dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__, |
1186 | dev->match_id); | |
75c86e74 | 1187 | |
7626e78d GL |
1188 | if (!dev->core.driver) { |
1189 | dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__, | |
1190 | __LINE__); | |
c4e6752d | 1191 | mutex_unlock(&vuart_bus_priv.probe_mutex); |
7626e78d GL |
1192 | return 0; |
1193 | } | |
74e95d5d | 1194 | |
7626e78d | 1195 | drv = ps3_system_bus_dev_to_vuart_drv(dev); |
74e95d5d | 1196 | |
7626e78d | 1197 | BUG_ON(!drv); |
74e95d5d | 1198 | |
7626e78d GL |
1199 | if (drv->shutdown) |
1200 | drv->shutdown(dev); | |
1201 | else if (drv->remove) { | |
1202 | dev_dbg(&dev->core, "%s:%d: no shutdown, calling remove\n", | |
1203 | __func__, __LINE__); | |
1204 | drv->remove(dev); | |
1205 | } else { | |
1206 | dev_dbg(&dev->core, "%s:%d: no shutdown method\n", __func__, | |
1207 | __LINE__); | |
1208 | BUG(); | |
1209 | } | |
74e95d5d | 1210 | |
7626e78d | 1211 | ps3_vuart_cleanup(dev); |
75c86e74 | 1212 | |
7626e78d | 1213 | dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__); |
75c86e74 | 1214 | |
c4e6752d | 1215 | mutex_unlock(&vuart_bus_priv.probe_mutex); |
7626e78d | 1216 | return 0; |
74e95d5d GL |
1217 | } |
1218 | ||
7626e78d | 1219 | static int __init ps3_vuart_bus_init(void) |
74e95d5d | 1220 | { |
7626e78d | 1221 | pr_debug("%s:%d:\n", __func__, __LINE__); |
75c86e74 | 1222 | |
7626e78d GL |
1223 | if (!firmware_has_feature(FW_FEATURE_PS3_LV1)) |
1224 | return -ENODEV; | |
74e95d5d | 1225 | |
c4e6752d | 1226 | mutex_init(&vuart_bus_priv.probe_mutex); |
74e95d5d | 1227 | |
7626e78d GL |
1228 | return 0; |
1229 | } | |
74e95d5d | 1230 | |
7626e78d GL |
1231 | static void __exit ps3_vuart_bus_exit(void) |
1232 | { | |
1233 | pr_debug("%s:%d:\n", __func__, __LINE__); | |
74e95d5d GL |
1234 | } |
1235 | ||
7626e78d GL |
1236 | core_initcall(ps3_vuart_bus_init); |
1237 | module_exit(ps3_vuart_bus_exit); | |
74e95d5d GL |
1238 | |
1239 | /** | |
1240 | * ps3_vuart_port_driver_register - Add a vuart port device driver. | |
1241 | */ | |
1242 | ||
1243 | int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver *drv) | |
1244 | { | |
1245 | int result; | |
1246 | ||
7626e78d GL |
1247 | pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name); |
1248 | ||
1249 | BUG_ON(!drv->core.match_id); | |
1250 | BUG_ON(!drv->core.core.name); | |
1251 | ||
1252 | drv->core.probe = ps3_vuart_probe; | |
1253 | drv->core.remove = ps3_vuart_remove; | |
1254 | drv->core.shutdown = ps3_vuart_shutdown; | |
1255 | ||
1256 | result = ps3_system_bus_driver_register(&drv->core); | |
74e95d5d GL |
1257 | return result; |
1258 | } | |
74e95d5d GL |
1259 | EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register); |
1260 | ||
1261 | /** | |
1262 | * ps3_vuart_port_driver_unregister - Remove a vuart port device driver. | |
1263 | */ | |
1264 | ||
1265 | void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver *drv) | |
1266 | { | |
7626e78d GL |
1267 | pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name); |
1268 | ps3_system_bus_driver_unregister(&drv->core); | |
74e95d5d | 1269 | } |
74e95d5d | 1270 | EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister); |