Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 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> |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 24 | #include <linux/workqueue.h> |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 25 | #include <asm/ps3.h> |
| 26 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 27 | #include <asm/firmware.h> |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 28 | #include <asm/lv1call.h> |
| 29 | #include <asm/bitops.h> |
| 30 | |
| 31 | #include "vuart.h" |
| 32 | |
| 33 | MODULE_AUTHOR("Sony Corporation"); |
| 34 | MODULE_LICENSE("GPL v2"); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 35 | MODULE_DESCRIPTION("PS3 vuart"); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 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 | |
| 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 | |
| 80 | struct 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 |
| 89 | static 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__) |
| 94 | static 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 | |
| 100 | static 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__) |
| 118 | static 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 | |
| 150 | struct vuart_triggers { |
| 151 | unsigned long rx; |
| 152 | unsigned long tx; |
| 153 | }; |
| 154 | |
| 155 | int 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 162 | result = lv1_get_virtual_uart_param(dev->priv->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 163 | 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 171 | result = lv1_get_virtual_uart_param(dev->priv->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 172 | 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 180 | result = lv1_get_virtual_uart_param(dev->priv->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 181 | 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 | |
| 197 | int 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 203 | result = lv1_set_virtual_uart_param(dev->priv->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 204 | 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 212 | result = lv1_get_virtual_uart_param(dev->priv->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 213 | 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 221 | result = lv1_set_virtual_uart_param(dev->priv->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 222 | 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 | |
| 236 | static int ps3_vuart_get_rx_bytes_waiting(struct ps3_vuart_port_device *dev, |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 237 | u64 *bytes_waiting) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 238 | { |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 239 | int result = lv1_get_virtual_uart_param(dev->priv->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 240 | 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 | |
| 251 | static 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 258 | dev->priv->interrupt_mask = mask; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 259 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 260 | result = lv1_set_virtual_uart_param(dev->priv->port_number, |
| 261 | PARAM_INTERRUPT_MASK, dev->priv->interrupt_mask); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 262 | |
| 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 270 | static int ps3_vuart_get_interrupt_status(struct ps3_vuart_port_device *dev, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 271 | unsigned long *status) |
| 272 | { |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 273 | u64 tmp; |
| 274 | int result = lv1_get_virtual_uart_param(dev->priv->port_number, |
| 275 | PARAM_INTERRUPT_STATUS, &tmp); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 276 | |
| 277 | if (result) |
| 278 | dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n", |
| 279 | __func__, __LINE__, ps3_result(result)); |
| 280 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 281 | *status = tmp & dev->priv->interrupt_mask; |
| 282 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 283 | dev_dbg(&dev->core, "%s:%d: m %lxh, s %lxh, m&s %lxh\n", |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 284 | __func__, __LINE__, dev->priv->interrupt_mask, tmp, *status); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 285 | |
| 286 | return result; |
| 287 | } |
| 288 | |
| 289 | int ps3_vuart_enable_interrupt_tx(struct ps3_vuart_port_device *dev) |
| 290 | { |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 291 | return (dev->priv->interrupt_mask & INTERRUPT_MASK_TX) ? 0 |
| 292 | : ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 293 | | INTERRUPT_MASK_TX); |
| 294 | } |
| 295 | |
| 296 | int ps3_vuart_enable_interrupt_rx(struct ps3_vuart_port_device *dev) |
| 297 | { |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 298 | return (dev->priv->interrupt_mask & INTERRUPT_MASK_RX) ? 0 |
| 299 | : ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 300 | | INTERRUPT_MASK_RX); |
| 301 | } |
| 302 | |
| 303 | int ps3_vuart_enable_interrupt_disconnect(struct ps3_vuart_port_device *dev) |
| 304 | { |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 305 | return (dev->priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) ? 0 |
| 306 | : ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 307 | | INTERRUPT_MASK_DISCONNECT); |
| 308 | } |
| 309 | |
| 310 | int ps3_vuart_disable_interrupt_tx(struct ps3_vuart_port_device *dev) |
| 311 | { |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 312 | return (dev->priv->interrupt_mask & INTERRUPT_MASK_TX) |
| 313 | ? ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 314 | & ~INTERRUPT_MASK_TX) : 0; |
| 315 | } |
| 316 | |
| 317 | int ps3_vuart_disable_interrupt_rx(struct ps3_vuart_port_device *dev) |
| 318 | { |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 319 | return (dev->priv->interrupt_mask & INTERRUPT_MASK_RX) |
| 320 | ? ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 321 | & ~INTERRUPT_MASK_RX) : 0; |
| 322 | } |
| 323 | |
| 324 | int ps3_vuart_disable_interrupt_disconnect(struct ps3_vuart_port_device *dev) |
| 325 | { |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 326 | return (dev->priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) |
| 327 | ? ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 328 | & ~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 | |
| 337 | static 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 342 | result = lv1_write_virtual_uart(dev->priv->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 343 | 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 351 | dev->priv->stats.bytes_written += *bytes_written; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 352 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 353 | dev_dbg(&dev->core, "%s:%d: wrote %lxh/%xh=>%lxh\n", __func__, __LINE__, |
| 354 | *bytes_written, bytes, dev->priv->stats.bytes_written); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 355 | |
| 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 | |
| 365 | static 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 372 | result = lv1_read_virtual_uart(dev->priv->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 373 | 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 381 | dev->priv->stats.bytes_read += *bytes_read; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 382 | |
| 383 | dev_dbg(&dev->core, "%s:%d: read %lxh/%xh=>%lxh\n", __func__, __LINE__, |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 384 | *bytes_read, bytes, dev->priv->stats.bytes_read); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 385 | |
| 386 | return result; |
| 387 | } |
| 388 | |
| 389 | /** |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 390 | * 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 | |
| 396 | void 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 Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 433 | * struct list_buffer - An element for a port device fifo buffer list. |
| 434 | */ |
| 435 | |
| 436 | struct 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 | |
| 453 | int 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 464 | spin_lock_irqsave(&dev->priv->tx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 465 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 466 | if (list_empty(&dev->priv->tx_list.head)) { |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 467 | unsigned long bytes_written; |
| 468 | |
| 469 | result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written); |
| 470 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 471 | spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 472 | |
| 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 489 | spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 490 | |
| 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 502 | spin_lock_irqsave(&dev->priv->tx_list.lock, flags); |
| 503 | list_add_tail(&lb->link, &dev->priv->tx_list.head); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 504 | ps3_vuart_enable_interrupt_tx(dev); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 505 | spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 506 | |
| 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 | |
| 522 | int 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 532 | spin_lock_irqsave(&dev->priv->rx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 533 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 534 | if (dev->priv->rx_list.bytes_held < bytes) { |
| 535 | spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 536 | dev_dbg(&dev->core, "%s:%d: starved for %lxh bytes\n", |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 537 | __func__, __LINE__, |
| 538 | bytes - dev->priv->rx_list.bytes_held); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 539 | return -EAGAIN; |
| 540 | } |
| 541 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 542 | list_for_each_entry_safe(lb, n, &dev->priv->rx_list.head, link) { |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 543 | 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 548 | dev->priv->rx_list.bytes_held -= bytes_read; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 549 | |
| 550 | if (bytes_read < lb->tail - lb->head) { |
| 551 | lb->head += bytes_read; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 552 | 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 Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 556 | return 0; |
| 557 | } |
| 558 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 559 | dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh " |
| 560 | "bytes\n", __func__, __LINE__, lb->dbg_number, |
| 561 | bytes_read); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 562 | |
| 563 | list_del(&lb->link); |
| 564 | kfree(lb); |
| 565 | } |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 566 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 567 | spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 568 | return 0; |
| 569 | } |
| 570 | |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 571 | int 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 | |
| 604 | void ps3_vuart_cancel_async(struct ps3_vuart_port_device *dev) |
| 605 | { |
| 606 | dev->priv->work.trigger = 0; |
| 607 | } |
| 608 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 609 | /** |
| 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 | |
| 617 | static 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 626 | spin_lock_irqsave(&dev->priv->tx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 627 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 628 | list_for_each_entry_safe(lb, n, &dev->priv->tx_list.head, link) { |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 629 | |
| 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); |
| 661 | port_full: |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 662 | spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 663 | 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 | |
| 676 | static 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 693 | /* Add some extra space for recently arrived data. */ |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 694 | |
| 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 708 | 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 Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 712 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 713 | dev_dbg(&dev->core, "%s:%d: buf_%lu: queued %lxh bytes\n", |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 714 | __func__, __LINE__, lb->dbg_number, bytes); |
| 715 | |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 716 | 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 Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | static 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 | |
| 744 | static int ps3_vuart_handle_port_interrupt(struct ps3_vuart_port_device *dev) |
| 745 | { |
| 746 | int result; |
| 747 | unsigned long status; |
| 748 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 749 | result = ps3_vuart_get_interrupt_status(dev, &status); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 750 | |
| 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 758 | dev->priv->stats.disconnect_interrupts++; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 759 | 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 765 | dev->priv->stats.tx_interrupts++; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 766 | 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 772 | dev->priv->stats.rx_interrupts++; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 773 | 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 781 | struct vuart_bus_priv { |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 782 | const struct ports_bmp bmp; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 783 | 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 Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 788 | |
| 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 | |
| 797 | static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private) |
| 798 | { |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 799 | struct vuart_bus_priv *bus_priv; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 800 | |
| 801 | BUG_ON(!_private); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 802 | bus_priv = (struct vuart_bus_priv *)_private; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 803 | |
| 804 | while (1) { |
| 805 | unsigned int port; |
| 806 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 807 | dump_ports_bmp(&bus_priv->bmp); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 808 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 809 | port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp.status); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 810 | |
| 811 | if (port == BITS_PER_LONG) |
| 812 | break; |
| 813 | |
| 814 | BUG_ON(port >= PORT_COUNT); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 815 | BUG_ON(!bus_priv->devices[port]); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 816 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 817 | ps3_vuart_handle_port_interrupt(bus_priv->devices[port]); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | return IRQ_HANDLED; |
| 821 | } |
| 822 | |
| 823 | static 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 Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 838 | static int ps3_vuart_probe(struct device *_dev) |
| 839 | { |
| 840 | int result; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 841 | unsigned int port_number; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 842 | 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 850 | 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 Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 856 | |
| 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 864 | if (vuart_bus_priv.devices[port_number]) { |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 865 | dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__, |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 866 | __LINE__, port_number); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 867 | result = -EBUSY; |
| 868 | goto fail_match; |
| 869 | } |
| 870 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 871 | vuart_bus_priv.devices[port_number] = dev; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 872 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 873 | /* Setup dev->priv. */ |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 874 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 875 | 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 Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 890 | 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 895 | if (++vuart_bus_priv.use_count == 1) { |
| 896 | |
Geoff Levand | 861be32 | 2007-01-26 19:08:08 -0800 | [diff] [blame] | 897 | result = ps3_alloc_vuart_irq(PS3_BINDING_CPU_ANY, |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 898 | (void*)&vuart_bus_priv.bmp.status, &vuart_bus_priv.virq); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 899 | |
| 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 908 | result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler, |
| 909 | IRQF_DISABLED, "vuart", &vuart_bus_priv); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 910 | |
| 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 Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 918 | /* clear stale pending interrupts */ |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 919 | |
| 920 | ps3_vuart_clear_rx_bytes(dev, 0); |
| 921 | |
| 922 | ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 923 | |
| 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 937 | down(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 938 | goto fail_probe; |
| 939 | } |
| 940 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 941 | up(&vuart_bus_priv.probe_mutex); |
| 942 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 943 | return result; |
| 944 | |
| 945 | fail_probe: |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 946 | ps3_vuart_set_interrupt_mask(dev, 0); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 947 | fail_request_irq: |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 948 | ps3_free_vuart_irq(vuart_bus_priv.virq); |
| 949 | vuart_bus_priv.virq = NO_IRQ; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 950 | fail_alloc_irq: |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 951 | --vuart_bus_priv.use_count; |
| 952 | kfree(dev->priv); |
| 953 | dev->priv = NULL; |
| 954 | fail_alloc: |
Al Viro | 89952d1 | 2007-03-14 09:17:59 +0000 | [diff] [blame] | 955 | vuart_bus_priv.devices[port_number] = NULL; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 956 | fail_match: |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 957 | up(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 958 | dev_dbg(&dev->core, "%s:%d failed\n", __func__, __LINE__); |
| 959 | return result; |
| 960 | } |
| 961 | |
| 962 | static 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 968 | down(&vuart_bus_priv.probe_mutex); |
| 969 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 970 | dev_dbg(&dev->core, "%s:%d: %s\n", __func__, __LINE__, |
| 971 | dev->core.bus_id); |
| 972 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 973 | BUG_ON(vuart_bus_priv.use_count < 1); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 974 | |
| 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 | |
Al Viro | 89952d1 | 2007-03-14 09:17:59 +0000 | [diff] [blame] | 981 | vuart_bus_priv.devices[dev->priv->port_number] = NULL; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 982 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 983 | 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 Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 988 | } |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 989 | |
| 990 | kfree(dev->priv); |
| 991 | dev->priv = NULL; |
| 992 | |
| 993 | up(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 994 | return 0; |
| 995 | } |
| 996 | |
Geert Uytterhoeven | 5b8e8ee | 2007-02-12 00:55:15 -0800 | [diff] [blame] | 997 | static 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 Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1013 | /** |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1014 | * ps3_vuart_bus - The vuart bus instance. |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1015 | * |
| 1016 | * The vuart is managed as a bus that port devices connect to. |
| 1017 | */ |
| 1018 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1019 | struct bus_type ps3_vuart_bus = { |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1020 | .name = "ps3_vuart", |
| 1021 | .match = ps3_vuart_match, |
| 1022 | .probe = ps3_vuart_probe, |
| 1023 | .remove = ps3_vuart_remove, |
Geert Uytterhoeven | 5b8e8ee | 2007-02-12 00:55:15 -0800 | [diff] [blame] | 1024 | .shutdown = ps3_vuart_shutdown, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1025 | }; |
| 1026 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1027 | int __init ps3_vuart_bus_init(void) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1028 | { |
| 1029 | int result; |
| 1030 | |
| 1031 | pr_debug("%s:%d:\n", __func__, __LINE__); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1032 | |
| 1033 | if (!firmware_has_feature(FW_FEATURE_PS3_LV1)) |
Geert Uytterhoeven | ef596c6 | 2007-03-10 00:05:38 +0100 | [diff] [blame] | 1034 | return -ENODEV; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1035 | |
| 1036 | init_MUTEX(&vuart_bus_priv.probe_mutex); |
| 1037 | result = bus_register(&ps3_vuart_bus); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1038 | BUG_ON(result); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1039 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1040 | return result; |
| 1041 | } |
| 1042 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1043 | void __exit ps3_vuart_bus_exit(void) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1044 | { |
| 1045 | pr_debug("%s:%d:\n", __func__, __LINE__); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1046 | bus_unregister(&ps3_vuart_bus); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1047 | } |
| 1048 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1049 | core_initcall(ps3_vuart_bus_init); |
| 1050 | module_exit(ps3_vuart_bus_exit); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1051 | |
| 1052 | /** |
| 1053 | * ps3_vuart_port_release_device - Remove a vuart port device. |
| 1054 | */ |
| 1055 | |
| 1056 | static void ps3_vuart_port_release_device(struct device *_dev) |
| 1057 | { |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1058 | #if defined(DEBUG) |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1059 | 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 Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1065 | #endif |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | /** |
| 1069 | * ps3_vuart_port_device_register - Add a vuart port device. |
| 1070 | */ |
| 1071 | |
| 1072 | int ps3_vuart_port_device_register(struct ps3_vuart_port_device *dev) |
| 1073 | { |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1074 | static unsigned int dev_count = 1; |
| 1075 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1076 | BUG_ON(dev->priv && "forgot to free"); |
| 1077 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1078 | dev->core.parent = NULL; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1079 | dev->core.bus = &ps3_vuart_bus; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1080 | 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1087 | return device_register(&dev->core); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | EXPORT_SYMBOL_GPL(ps3_vuart_port_device_register); |
| 1091 | |
| 1092 | /** |
| 1093 | * ps3_vuart_port_driver_register - Add a vuart port device driver. |
| 1094 | */ |
| 1095 | |
| 1096 | int 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 Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1101 | drv->core.bus = &ps3_vuart_bus; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1102 | result = driver_register(&drv->core); |
| 1103 | return result; |
| 1104 | } |
| 1105 | |
| 1106 | EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register); |
| 1107 | |
| 1108 | /** |
| 1109 | * ps3_vuart_port_driver_unregister - Remove a vuart port device driver. |
| 1110 | */ |
| 1111 | |
| 1112 | void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver *drv) |
| 1113 | { |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1114 | pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.name); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1115 | driver_unregister(&drv->core); |
| 1116 | } |
| 1117 | |
| 1118 | EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister); |