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> |
| 24 | #include <asm/ps3.h> |
| 25 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 26 | #include <asm/firmware.h> |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 27 | #include <asm/lv1call.h> |
| 28 | #include <asm/bitops.h> |
| 29 | |
| 30 | #include "vuart.h" |
| 31 | |
| 32 | MODULE_AUTHOR("Sony Corporation"); |
| 33 | MODULE_LICENSE("GPL v2"); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 34 | MODULE_DESCRIPTION("PS3 vuart"); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * vuart - An inter-partition data link service. |
| 38 | * port 0: PS3 AV Settings. |
| 39 | * port 2: PS3 System Manager. |
| 40 | * |
| 41 | * The vuart provides a bi-directional byte stream data link between logical |
| 42 | * partitions. Its primary role is as a communications link between the guest |
| 43 | * OS and the system policy module. The current HV does not support any |
| 44 | * connections other than those listed. |
| 45 | */ |
| 46 | |
| 47 | enum {PORT_COUNT = 3,}; |
| 48 | |
| 49 | enum vuart_param { |
| 50 | PARAM_TX_TRIGGER = 0, |
| 51 | PARAM_RX_TRIGGER = 1, |
| 52 | PARAM_INTERRUPT_MASK = 2, |
| 53 | PARAM_RX_BUF_SIZE = 3, /* read only */ |
| 54 | PARAM_RX_BYTES = 4, /* read only */ |
| 55 | PARAM_TX_BUF_SIZE = 5, /* read only */ |
| 56 | PARAM_TX_BYTES = 6, /* read only */ |
| 57 | PARAM_INTERRUPT_STATUS = 7, /* read only */ |
| 58 | }; |
| 59 | |
| 60 | enum vuart_interrupt_bit { |
| 61 | INTERRUPT_BIT_TX = 0, |
| 62 | INTERRUPT_BIT_RX = 1, |
| 63 | INTERRUPT_BIT_DISCONNECT = 2, |
| 64 | }; |
| 65 | |
| 66 | enum vuart_interrupt_mask { |
| 67 | INTERRUPT_MASK_TX = 1, |
| 68 | INTERRUPT_MASK_RX = 2, |
| 69 | INTERRUPT_MASK_DISCONNECT = 4, |
| 70 | }; |
| 71 | |
| 72 | /** |
| 73 | * struct ports_bmp - bitmap indicating ports needing service. |
| 74 | * |
| 75 | * A 256 bit read only bitmap indicating ports needing service. Do not write |
| 76 | * to these bits. Must not cross a page boundary. |
| 77 | */ |
| 78 | |
| 79 | struct ports_bmp { |
| 80 | u64 status; |
| 81 | u64 unused[3]; |
| 82 | } __attribute__ ((aligned (32))); |
| 83 | |
| 84 | /* redefine dev_dbg to do a syntax check */ |
| 85 | |
| 86 | #if !defined(DEBUG) |
| 87 | #undef dev_dbg |
| 88 | static inline int __attribute__ ((format (printf, 2, 3))) dev_dbg( |
| 89 | const struct device *_dev, const char *fmt, ...) {return 0;} |
| 90 | #endif |
| 91 | |
| 92 | #define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__) |
| 93 | static void __attribute__ ((unused)) _dump_ports_bmp( |
| 94 | const struct ports_bmp* bmp, const char* func, int line) |
| 95 | { |
| 96 | pr_debug("%s:%d: ports_bmp: %016lxh\n", func, line, bmp->status); |
| 97 | } |
| 98 | |
| 99 | static int ps3_vuart_match_id_to_port(enum ps3_match_id match_id, |
| 100 | unsigned int *port_number) |
| 101 | { |
| 102 | switch(match_id) { |
| 103 | case PS3_MATCH_ID_AV_SETTINGS: |
| 104 | *port_number = 0; |
| 105 | return 0; |
| 106 | case PS3_MATCH_ID_SYSTEM_MANAGER: |
| 107 | *port_number = 2; |
| 108 | return 0; |
| 109 | default: |
| 110 | WARN_ON(1); |
| 111 | *port_number = UINT_MAX; |
| 112 | return -EINVAL; |
| 113 | }; |
| 114 | } |
| 115 | |
| 116 | #define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__) |
| 117 | static void __attribute__ ((unused)) _dump_port_params(unsigned int port_number, |
| 118 | const char* func, int line) |
| 119 | { |
| 120 | #if defined(DEBUG) |
| 121 | static const char *strings[] = { |
| 122 | "tx_trigger ", |
| 123 | "rx_trigger ", |
| 124 | "interrupt_mask ", |
| 125 | "rx_buf_size ", |
| 126 | "rx_bytes ", |
| 127 | "tx_buf_size ", |
| 128 | "tx_bytes ", |
| 129 | "interrupt_status", |
| 130 | }; |
| 131 | int result; |
| 132 | unsigned int i; |
| 133 | u64 value; |
| 134 | |
| 135 | for (i = 0; i < ARRAY_SIZE(strings); i++) { |
| 136 | result = lv1_get_virtual_uart_param(port_number, i, &value); |
| 137 | |
| 138 | if (result) { |
| 139 | pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line, |
| 140 | port_number, strings[i], ps3_result(result)); |
| 141 | continue; |
| 142 | } |
| 143 | pr_debug("%s:%d: port_%u: %s = %lxh\n", |
| 144 | func, line, port_number, strings[i], value); |
| 145 | } |
| 146 | #endif |
| 147 | } |
| 148 | |
| 149 | struct vuart_triggers { |
| 150 | unsigned long rx; |
| 151 | unsigned long tx; |
| 152 | }; |
| 153 | |
| 154 | int ps3_vuart_get_triggers(struct ps3_vuart_port_device *dev, |
| 155 | struct vuart_triggers *trig) |
| 156 | { |
| 157 | int result; |
| 158 | unsigned long size; |
| 159 | unsigned long val; |
| 160 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 161 | result = lv1_get_virtual_uart_param(dev->priv->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 162 | PARAM_TX_TRIGGER, &trig->tx); |
| 163 | |
| 164 | if (result) { |
| 165 | dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n", |
| 166 | __func__, __LINE__, ps3_result(result)); |
| 167 | return result; |
| 168 | } |
| 169 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 170 | result = lv1_get_virtual_uart_param(dev->priv->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 171 | PARAM_RX_BUF_SIZE, &size); |
| 172 | |
| 173 | if (result) { |
| 174 | dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n", |
| 175 | __func__, __LINE__, ps3_result(result)); |
| 176 | return result; |
| 177 | } |
| 178 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 179 | result = lv1_get_virtual_uart_param(dev->priv->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 180 | PARAM_RX_TRIGGER, &val); |
| 181 | |
| 182 | if (result) { |
| 183 | dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n", |
| 184 | __func__, __LINE__, ps3_result(result)); |
| 185 | return result; |
| 186 | } |
| 187 | |
| 188 | trig->rx = size - val; |
| 189 | |
| 190 | dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__, |
| 191 | trig->tx, trig->rx); |
| 192 | |
| 193 | return result; |
| 194 | } |
| 195 | |
| 196 | int ps3_vuart_set_triggers(struct ps3_vuart_port_device *dev, unsigned int tx, |
| 197 | unsigned int rx) |
| 198 | { |
| 199 | int result; |
| 200 | unsigned long size; |
| 201 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 202 | result = lv1_set_virtual_uart_param(dev->priv->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 203 | PARAM_TX_TRIGGER, tx); |
| 204 | |
| 205 | if (result) { |
| 206 | dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n", |
| 207 | __func__, __LINE__, ps3_result(result)); |
| 208 | return result; |
| 209 | } |
| 210 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 211 | result = lv1_get_virtual_uart_param(dev->priv->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 212 | PARAM_RX_BUF_SIZE, &size); |
| 213 | |
| 214 | if (result) { |
| 215 | dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n", |
| 216 | __func__, __LINE__, ps3_result(result)); |
| 217 | return result; |
| 218 | } |
| 219 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 220 | result = lv1_set_virtual_uart_param(dev->priv->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 221 | PARAM_RX_TRIGGER, size - rx); |
| 222 | |
| 223 | if (result) { |
| 224 | dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n", |
| 225 | __func__, __LINE__, ps3_result(result)); |
| 226 | return result; |
| 227 | } |
| 228 | |
| 229 | dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__, |
| 230 | tx, rx); |
| 231 | |
| 232 | return result; |
| 233 | } |
| 234 | |
| 235 | 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^] | 236 | u64 *bytes_waiting) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 237 | { |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 238 | int result = lv1_get_virtual_uart_param(dev->priv->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 239 | PARAM_RX_BYTES, bytes_waiting); |
| 240 | |
| 241 | if (result) |
| 242 | dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n", |
| 243 | __func__, __LINE__, ps3_result(result)); |
| 244 | |
| 245 | dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, |
| 246 | *bytes_waiting); |
| 247 | return result; |
| 248 | } |
| 249 | |
| 250 | static int ps3_vuart_set_interrupt_mask(struct ps3_vuart_port_device *dev, |
| 251 | unsigned long mask) |
| 252 | { |
| 253 | int result; |
| 254 | |
| 255 | dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, mask); |
| 256 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 257 | dev->priv->interrupt_mask = mask; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 258 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 259 | result = lv1_set_virtual_uart_param(dev->priv->port_number, |
| 260 | PARAM_INTERRUPT_MASK, dev->priv->interrupt_mask); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 261 | |
| 262 | if (result) |
| 263 | dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n", |
| 264 | __func__, __LINE__, ps3_result(result)); |
| 265 | |
| 266 | return result; |
| 267 | } |
| 268 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 269 | 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] | 270 | unsigned long *status) |
| 271 | { |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 272 | u64 tmp; |
| 273 | int result = lv1_get_virtual_uart_param(dev->priv->port_number, |
| 274 | PARAM_INTERRUPT_STATUS, &tmp); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 275 | |
| 276 | if (result) |
| 277 | dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n", |
| 278 | __func__, __LINE__, ps3_result(result)); |
| 279 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 280 | *status = tmp & dev->priv->interrupt_mask; |
| 281 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 282 | 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^] | 283 | __func__, __LINE__, dev->priv->interrupt_mask, tmp, *status); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 284 | |
| 285 | return result; |
| 286 | } |
| 287 | |
| 288 | int ps3_vuart_enable_interrupt_tx(struct ps3_vuart_port_device *dev) |
| 289 | { |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 290 | return (dev->priv->interrupt_mask & INTERRUPT_MASK_TX) ? 0 |
| 291 | : ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 292 | | INTERRUPT_MASK_TX); |
| 293 | } |
| 294 | |
| 295 | int ps3_vuart_enable_interrupt_rx(struct ps3_vuart_port_device *dev) |
| 296 | { |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 297 | return (dev->priv->interrupt_mask & INTERRUPT_MASK_RX) ? 0 |
| 298 | : ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 299 | | INTERRUPT_MASK_RX); |
| 300 | } |
| 301 | |
| 302 | int ps3_vuart_enable_interrupt_disconnect(struct ps3_vuart_port_device *dev) |
| 303 | { |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 304 | return (dev->priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) ? 0 |
| 305 | : ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 306 | | INTERRUPT_MASK_DISCONNECT); |
| 307 | } |
| 308 | |
| 309 | int ps3_vuart_disable_interrupt_tx(struct ps3_vuart_port_device *dev) |
| 310 | { |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 311 | return (dev->priv->interrupt_mask & INTERRUPT_MASK_TX) |
| 312 | ? ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 313 | & ~INTERRUPT_MASK_TX) : 0; |
| 314 | } |
| 315 | |
| 316 | int ps3_vuart_disable_interrupt_rx(struct ps3_vuart_port_device *dev) |
| 317 | { |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 318 | return (dev->priv->interrupt_mask & INTERRUPT_MASK_RX) |
| 319 | ? ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 320 | & ~INTERRUPT_MASK_RX) : 0; |
| 321 | } |
| 322 | |
| 323 | int ps3_vuart_disable_interrupt_disconnect(struct ps3_vuart_port_device *dev) |
| 324 | { |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 325 | return (dev->priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) |
| 326 | ? ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 327 | & ~INTERRUPT_MASK_DISCONNECT) : 0; |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * ps3_vuart_raw_write - Low level write helper. |
| 332 | * |
| 333 | * Do not call ps3_vuart_raw_write directly, use ps3_vuart_write. |
| 334 | */ |
| 335 | |
| 336 | static int ps3_vuart_raw_write(struct ps3_vuart_port_device *dev, |
| 337 | const void* buf, unsigned int bytes, unsigned long *bytes_written) |
| 338 | { |
| 339 | int result; |
| 340 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 341 | result = lv1_write_virtual_uart(dev->priv->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 342 | ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written); |
| 343 | |
| 344 | if (result) { |
| 345 | dev_dbg(&dev->core, "%s:%d: lv1_write_virtual_uart failed: " |
| 346 | "%s\n", __func__, __LINE__, ps3_result(result)); |
| 347 | return result; |
| 348 | } |
| 349 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 350 | dev->priv->stats.bytes_written += *bytes_written; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 351 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 352 | dev_dbg(&dev->core, "%s:%d: wrote %lxh/%xh=>%lxh\n", __func__, __LINE__, |
| 353 | *bytes_written, bytes, dev->priv->stats.bytes_written); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 354 | |
| 355 | return result; |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * ps3_vuart_raw_read - Low level read helper. |
| 360 | * |
| 361 | * Do not call ps3_vuart_raw_read directly, use ps3_vuart_read. |
| 362 | */ |
| 363 | |
| 364 | static int ps3_vuart_raw_read(struct ps3_vuart_port_device *dev, void* buf, |
| 365 | unsigned int bytes, unsigned long *bytes_read) |
| 366 | { |
| 367 | int result; |
| 368 | |
| 369 | dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes); |
| 370 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 371 | result = lv1_read_virtual_uart(dev->priv->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 372 | ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_read); |
| 373 | |
| 374 | if (result) { |
| 375 | dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n", |
| 376 | __func__, __LINE__, ps3_result(result)); |
| 377 | return result; |
| 378 | } |
| 379 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 380 | dev->priv->stats.bytes_read += *bytes_read; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 381 | |
| 382 | 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^] | 383 | *bytes_read, bytes, dev->priv->stats.bytes_read); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 384 | |
| 385 | return result; |
| 386 | } |
| 387 | |
| 388 | /** |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 389 | * ps3_vuart_clear_rx_bytes - Discard bytes received. |
| 390 | * @bytes: Max byte count to discard, zero = all pending. |
| 391 | * |
| 392 | * Used to clear pending rx interrupt source. Will not block. |
| 393 | */ |
| 394 | |
| 395 | void ps3_vuart_clear_rx_bytes(struct ps3_vuart_port_device *dev, |
| 396 | unsigned int bytes) |
| 397 | { |
| 398 | int result; |
| 399 | u64 bytes_waiting; |
| 400 | void* tmp; |
| 401 | |
| 402 | result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting); |
| 403 | |
| 404 | BUG_ON(result); |
| 405 | |
| 406 | bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting; |
| 407 | |
| 408 | dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes); |
| 409 | |
| 410 | if (!bytes) |
| 411 | return; |
| 412 | |
| 413 | /* Add some extra space for recently arrived data. */ |
| 414 | |
| 415 | bytes += 128; |
| 416 | |
| 417 | tmp = kmalloc(bytes, GFP_KERNEL); |
| 418 | |
| 419 | if (!tmp) |
| 420 | return; |
| 421 | |
| 422 | ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting); |
| 423 | |
| 424 | kfree(tmp); |
| 425 | |
| 426 | /* Don't include these bytes in the stats. */ |
| 427 | |
| 428 | dev->priv->stats.bytes_read -= bytes_waiting; |
| 429 | } |
| 430 | |
| 431 | /** |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 432 | * struct list_buffer - An element for a port device fifo buffer list. |
| 433 | */ |
| 434 | |
| 435 | struct list_buffer { |
| 436 | struct list_head link; |
| 437 | const unsigned char *head; |
| 438 | const unsigned char *tail; |
| 439 | unsigned long dbg_number; |
| 440 | unsigned char data[]; |
| 441 | }; |
| 442 | |
| 443 | /** |
| 444 | * ps3_vuart_write - the entry point for writing data to a port |
| 445 | * |
| 446 | * If the port is idle on entry as much of the incoming data is written to |
| 447 | * the port as the port will accept. Otherwise a list buffer is created |
| 448 | * and any remaning incoming data is copied to that buffer. The buffer is |
| 449 | * then enqueued for transmision via the transmit interrupt. |
| 450 | */ |
| 451 | |
| 452 | int ps3_vuart_write(struct ps3_vuart_port_device *dev, const void* buf, |
| 453 | unsigned int bytes) |
| 454 | { |
| 455 | static unsigned long dbg_number; |
| 456 | int result; |
| 457 | unsigned long flags; |
| 458 | struct list_buffer *lb; |
| 459 | |
| 460 | dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__, |
| 461 | bytes, bytes); |
| 462 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 463 | spin_lock_irqsave(&dev->priv->tx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 464 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 465 | if (list_empty(&dev->priv->tx_list.head)) { |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 466 | unsigned long bytes_written; |
| 467 | |
| 468 | result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written); |
| 469 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 470 | spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 471 | |
| 472 | if (result) { |
| 473 | dev_dbg(&dev->core, |
| 474 | "%s:%d: ps3_vuart_raw_write failed\n", |
| 475 | __func__, __LINE__); |
| 476 | return result; |
| 477 | } |
| 478 | |
| 479 | if (bytes_written == bytes) { |
| 480 | dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n", |
| 481 | __func__, __LINE__, bytes); |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | bytes -= bytes_written; |
| 486 | buf += bytes_written; |
| 487 | } else |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 488 | spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 489 | |
| 490 | lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL); |
| 491 | |
| 492 | if (!lb) { |
| 493 | return -ENOMEM; |
| 494 | } |
| 495 | |
| 496 | memcpy(lb->data, buf, bytes); |
| 497 | lb->head = lb->data; |
| 498 | lb->tail = lb->data + bytes; |
| 499 | lb->dbg_number = ++dbg_number; |
| 500 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 501 | spin_lock_irqsave(&dev->priv->tx_list.lock, flags); |
| 502 | list_add_tail(&lb->link, &dev->priv->tx_list.head); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 503 | ps3_vuart_enable_interrupt_tx(dev); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 504 | spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 505 | |
| 506 | dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %xh bytes\n", |
| 507 | __func__, __LINE__, lb->dbg_number, bytes); |
| 508 | |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * ps3_vuart_read - the entry point for reading data from a port |
| 514 | * |
| 515 | * If enough bytes to satisfy the request are held in the buffer list those |
| 516 | * bytes are dequeued and copied to the caller's buffer. Emptied list buffers |
| 517 | * are retiered. If the request cannot be statified by bytes held in the list |
| 518 | * buffers -EAGAIN is returned. |
| 519 | */ |
| 520 | |
| 521 | int ps3_vuart_read(struct ps3_vuart_port_device *dev, void* buf, |
| 522 | unsigned int bytes) |
| 523 | { |
| 524 | unsigned long flags; |
| 525 | struct list_buffer *lb, *n; |
| 526 | unsigned long bytes_read; |
| 527 | |
| 528 | dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__, |
| 529 | bytes, bytes); |
| 530 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 531 | spin_lock_irqsave(&dev->priv->rx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 532 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 533 | if (dev->priv->rx_list.bytes_held < bytes) { |
| 534 | spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 535 | dev_dbg(&dev->core, "%s:%d: starved for %lxh bytes\n", |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 536 | __func__, __LINE__, |
| 537 | bytes - dev->priv->rx_list.bytes_held); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 538 | return -EAGAIN; |
| 539 | } |
| 540 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 541 | 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] | 542 | bytes_read = min((unsigned int)(lb->tail - lb->head), bytes); |
| 543 | |
| 544 | memcpy(buf, lb->head, bytes_read); |
| 545 | buf += bytes_read; |
| 546 | bytes -= bytes_read; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 547 | dev->priv->rx_list.bytes_held -= bytes_read; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 548 | |
| 549 | if (bytes_read < lb->tail - lb->head) { |
| 550 | lb->head += bytes_read; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 551 | dev_dbg(&dev->core, "%s:%d: buf_%lu: dequeued %lxh " |
| 552 | "bytes\n", __func__, __LINE__, lb->dbg_number, |
| 553 | bytes_read); |
| 554 | spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 555 | return 0; |
| 556 | } |
| 557 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 558 | dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh " |
| 559 | "bytes\n", __func__, __LINE__, lb->dbg_number, |
| 560 | bytes_read); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 561 | |
| 562 | list_del(&lb->link); |
| 563 | kfree(lb); |
| 564 | } |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 565 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 566 | spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * ps3_vuart_handle_interrupt_tx - third stage transmit interrupt handler |
| 572 | * |
| 573 | * Services the transmit interrupt for the port. Writes as much data from the |
| 574 | * buffer list as the port will accept. Retires any emptied list buffers and |
| 575 | * adjusts the final list buffer state for a partial write. |
| 576 | */ |
| 577 | |
| 578 | static int ps3_vuart_handle_interrupt_tx(struct ps3_vuart_port_device *dev) |
| 579 | { |
| 580 | int result = 0; |
| 581 | unsigned long flags; |
| 582 | struct list_buffer *lb, *n; |
| 583 | unsigned long bytes_total = 0; |
| 584 | |
| 585 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); |
| 586 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 587 | spin_lock_irqsave(&dev->priv->tx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 588 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 589 | 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] | 590 | |
| 591 | unsigned long bytes_written; |
| 592 | |
| 593 | result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head, |
| 594 | &bytes_written); |
| 595 | |
| 596 | if (result) { |
| 597 | dev_dbg(&dev->core, |
| 598 | "%s:%d: ps3_vuart_raw_write failed\n", |
| 599 | __func__, __LINE__); |
| 600 | break; |
| 601 | } |
| 602 | |
| 603 | bytes_total += bytes_written; |
| 604 | |
| 605 | if (bytes_written < lb->tail - lb->head) { |
| 606 | lb->head += bytes_written; |
| 607 | dev_dbg(&dev->core, |
| 608 | "%s:%d cleared buf_%lu, %lxh bytes\n", |
| 609 | __func__, __LINE__, lb->dbg_number, |
| 610 | bytes_written); |
| 611 | goto port_full; |
| 612 | } |
| 613 | |
| 614 | dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__, |
| 615 | lb->dbg_number); |
| 616 | |
| 617 | list_del(&lb->link); |
| 618 | kfree(lb); |
| 619 | } |
| 620 | |
| 621 | ps3_vuart_disable_interrupt_tx(dev); |
| 622 | port_full: |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 623 | spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 624 | dev_dbg(&dev->core, "%s:%d wrote %lxh bytes total\n", |
| 625 | __func__, __LINE__, bytes_total); |
| 626 | return result; |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * ps3_vuart_handle_interrupt_rx - third stage receive interrupt handler |
| 631 | * |
| 632 | * Services the receive interrupt for the port. Creates a list buffer and |
| 633 | * copies all waiting port data to that buffer and enqueues the buffer in the |
| 634 | * buffer list. Buffer list data is dequeued via ps3_vuart_read. |
| 635 | */ |
| 636 | |
| 637 | static int ps3_vuart_handle_interrupt_rx(struct ps3_vuart_port_device *dev) |
| 638 | { |
| 639 | static unsigned long dbg_number; |
| 640 | int result = 0; |
| 641 | unsigned long flags; |
| 642 | struct list_buffer *lb; |
| 643 | unsigned long bytes; |
| 644 | |
| 645 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); |
| 646 | |
| 647 | result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes); |
| 648 | |
| 649 | if (result) |
| 650 | return -EIO; |
| 651 | |
| 652 | BUG_ON(!bytes); |
| 653 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 654 | /* Add some extra space for recently arrived data. */ |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 655 | |
| 656 | bytes += 128; |
| 657 | |
| 658 | lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC); |
| 659 | |
| 660 | if (!lb) |
| 661 | return -ENOMEM; |
| 662 | |
| 663 | ps3_vuart_raw_read(dev, lb->data, bytes, &bytes); |
| 664 | |
| 665 | lb->head = lb->data; |
| 666 | lb->tail = lb->data + bytes; |
| 667 | lb->dbg_number = ++dbg_number; |
| 668 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 669 | spin_lock_irqsave(&dev->priv->rx_list.lock, flags); |
| 670 | list_add_tail(&lb->link, &dev->priv->rx_list.head); |
| 671 | dev->priv->rx_list.bytes_held += bytes; |
| 672 | spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 673 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 674 | dev_dbg(&dev->core, "%s:%d: buf_%lu: queued %lxh bytes\n", |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 675 | __func__, __LINE__, lb->dbg_number, bytes); |
| 676 | |
| 677 | return 0; |
| 678 | } |
| 679 | |
| 680 | static int ps3_vuart_handle_interrupt_disconnect( |
| 681 | struct ps3_vuart_port_device *dev) |
| 682 | { |
| 683 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); |
| 684 | BUG_ON("no support"); |
| 685 | return -1; |
| 686 | } |
| 687 | |
| 688 | /** |
| 689 | * ps3_vuart_handle_port_interrupt - second stage interrupt handler |
| 690 | * |
| 691 | * Services any pending interrupt types for the port. Passes control to the |
| 692 | * third stage type specific interrupt handler. Returns control to the first |
| 693 | * stage handler after one iteration. |
| 694 | */ |
| 695 | |
| 696 | static int ps3_vuart_handle_port_interrupt(struct ps3_vuart_port_device *dev) |
| 697 | { |
| 698 | int result; |
| 699 | unsigned long status; |
| 700 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 701 | result = ps3_vuart_get_interrupt_status(dev, &status); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 702 | |
| 703 | if (result) |
| 704 | return result; |
| 705 | |
| 706 | dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__, |
| 707 | status); |
| 708 | |
| 709 | if (status & INTERRUPT_MASK_DISCONNECT) { |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 710 | dev->priv->stats.disconnect_interrupts++; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 711 | result = ps3_vuart_handle_interrupt_disconnect(dev); |
| 712 | if (result) |
| 713 | ps3_vuart_disable_interrupt_disconnect(dev); |
| 714 | } |
| 715 | |
| 716 | if (status & INTERRUPT_MASK_TX) { |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 717 | dev->priv->stats.tx_interrupts++; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 718 | result = ps3_vuart_handle_interrupt_tx(dev); |
| 719 | if (result) |
| 720 | ps3_vuart_disable_interrupt_tx(dev); |
| 721 | } |
| 722 | |
| 723 | if (status & INTERRUPT_MASK_RX) { |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 724 | dev->priv->stats.rx_interrupts++; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 725 | result = ps3_vuart_handle_interrupt_rx(dev); |
| 726 | if (result) |
| 727 | ps3_vuart_disable_interrupt_rx(dev); |
| 728 | } |
| 729 | |
| 730 | return 0; |
| 731 | } |
| 732 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 733 | struct vuart_bus_priv { |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 734 | const struct ports_bmp bmp; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 735 | unsigned int virq; |
| 736 | struct semaphore probe_mutex; |
| 737 | int use_count; |
| 738 | struct ps3_vuart_port_device *devices[PORT_COUNT]; |
| 739 | } static vuart_bus_priv; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 740 | |
| 741 | /** |
| 742 | * ps3_vuart_irq_handler - first stage interrupt handler |
| 743 | * |
| 744 | * Loops finding any interrupting port and its associated instance data. |
| 745 | * Passes control to the second stage port specific interrupt handler. Loops |
| 746 | * until all outstanding interrupts are serviced. |
| 747 | */ |
| 748 | |
| 749 | static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private) |
| 750 | { |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 751 | struct vuart_bus_priv *bus_priv; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 752 | |
| 753 | BUG_ON(!_private); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 754 | bus_priv = (struct vuart_bus_priv *)_private; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 755 | |
| 756 | while (1) { |
| 757 | unsigned int port; |
| 758 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 759 | dump_ports_bmp(&bus_priv->bmp); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 760 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 761 | port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp.status); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 762 | |
| 763 | if (port == BITS_PER_LONG) |
| 764 | break; |
| 765 | |
| 766 | BUG_ON(port >= PORT_COUNT); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 767 | BUG_ON(!bus_priv->devices[port]); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 768 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 769 | ps3_vuart_handle_port_interrupt(bus_priv->devices[port]); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | return IRQ_HANDLED; |
| 773 | } |
| 774 | |
| 775 | static int ps3_vuart_match(struct device *_dev, struct device_driver *_drv) |
| 776 | { |
| 777 | int result; |
| 778 | struct ps3_vuart_port_driver *drv = to_ps3_vuart_port_driver(_drv); |
| 779 | struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev); |
| 780 | |
| 781 | result = dev->match_id == drv->match_id; |
| 782 | |
| 783 | dev_info(&dev->core, "%s:%d: dev=%u(%s), drv=%u(%s): %s\n", __func__, |
| 784 | __LINE__, dev->match_id, dev->core.bus_id, drv->match_id, |
| 785 | drv->core.name, (result ? "match" : "miss")); |
| 786 | |
| 787 | return result; |
| 788 | } |
| 789 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 790 | static int ps3_vuart_probe(struct device *_dev) |
| 791 | { |
| 792 | int result; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 793 | unsigned int port_number; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 794 | struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev); |
| 795 | struct ps3_vuart_port_driver *drv = |
| 796 | to_ps3_vuart_port_driver(_dev->driver); |
| 797 | |
| 798 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); |
| 799 | |
| 800 | BUG_ON(!drv); |
| 801 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 802 | down(&vuart_bus_priv.probe_mutex); |
| 803 | |
| 804 | /* Setup vuart_bus_priv.devices[]. */ |
| 805 | |
| 806 | result = ps3_vuart_match_id_to_port(dev->match_id, |
| 807 | &port_number); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 808 | |
| 809 | if (result) { |
| 810 | dev_dbg(&dev->core, "%s:%d: unknown match_id (%d)\n", |
| 811 | __func__, __LINE__, dev->match_id); |
| 812 | result = -EINVAL; |
| 813 | goto fail_match; |
| 814 | } |
| 815 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 816 | if (vuart_bus_priv.devices[port_number]) { |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 817 | dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__, |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 818 | __LINE__, port_number); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 819 | result = -EBUSY; |
| 820 | goto fail_match; |
| 821 | } |
| 822 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 823 | vuart_bus_priv.devices[port_number] = dev; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 824 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 825 | /* Setup dev->priv. */ |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 826 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 827 | dev->priv = kzalloc(sizeof(struct ps3_vuart_port_priv), GFP_KERNEL); |
| 828 | |
| 829 | if (!dev->priv) { |
| 830 | result = -ENOMEM; |
| 831 | goto fail_alloc; |
| 832 | } |
| 833 | |
| 834 | dev->priv->port_number = port_number; |
| 835 | |
| 836 | INIT_LIST_HEAD(&dev->priv->tx_list.head); |
| 837 | spin_lock_init(&dev->priv->tx_list.lock); |
| 838 | |
| 839 | INIT_LIST_HEAD(&dev->priv->rx_list.head); |
| 840 | spin_lock_init(&dev->priv->rx_list.lock); |
| 841 | |
| 842 | if (++vuart_bus_priv.use_count == 1) { |
| 843 | |
Geoff Levand | 861be32 | 2007-01-26 19:08:08 -0800 | [diff] [blame] | 844 | result = ps3_alloc_vuart_irq(PS3_BINDING_CPU_ANY, |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 845 | (void*)&vuart_bus_priv.bmp.status, &vuart_bus_priv.virq); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 846 | |
| 847 | if (result) { |
| 848 | dev_dbg(&dev->core, |
| 849 | "%s:%d: ps3_alloc_vuart_irq failed (%d)\n", |
| 850 | __func__, __LINE__, result); |
| 851 | result = -EPERM; |
| 852 | goto fail_alloc_irq; |
| 853 | } |
| 854 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 855 | result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler, |
| 856 | IRQF_DISABLED, "vuart", &vuart_bus_priv); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 857 | |
| 858 | if (result) { |
| 859 | dev_info(&dev->core, "%s:%d: request_irq failed (%d)\n", |
| 860 | __func__, __LINE__, result); |
| 861 | goto fail_request_irq; |
| 862 | } |
| 863 | } |
| 864 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 865 | /* clear stale pending interrupts */ |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 866 | |
| 867 | ps3_vuart_clear_rx_bytes(dev, 0); |
| 868 | |
| 869 | ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 870 | |
| 871 | ps3_vuart_set_triggers(dev, 1, 1); |
| 872 | |
| 873 | if (drv->probe) |
| 874 | result = drv->probe(dev); |
| 875 | else { |
| 876 | result = 0; |
| 877 | dev_info(&dev->core, "%s:%d: no probe method\n", __func__, |
| 878 | __LINE__); |
| 879 | } |
| 880 | |
| 881 | if (result) { |
| 882 | dev_dbg(&dev->core, "%s:%d: drv->probe failed\n", |
| 883 | __func__, __LINE__); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 884 | down(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 885 | goto fail_probe; |
| 886 | } |
| 887 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 888 | up(&vuart_bus_priv.probe_mutex); |
| 889 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 890 | return result; |
| 891 | |
| 892 | fail_probe: |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 893 | ps3_vuart_set_interrupt_mask(dev, 0); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 894 | fail_request_irq: |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 895 | ps3_free_vuart_irq(vuart_bus_priv.virq); |
| 896 | vuart_bus_priv.virq = NO_IRQ; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 897 | fail_alloc_irq: |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 898 | --vuart_bus_priv.use_count; |
| 899 | kfree(dev->priv); |
| 900 | dev->priv = NULL; |
| 901 | fail_alloc: |
| 902 | vuart_bus_priv.devices[port_number] = 0; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 903 | fail_match: |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 904 | up(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 905 | dev_dbg(&dev->core, "%s:%d failed\n", __func__, __LINE__); |
| 906 | return result; |
| 907 | } |
| 908 | |
| 909 | static int ps3_vuart_remove(struct device *_dev) |
| 910 | { |
| 911 | struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev); |
| 912 | struct ps3_vuart_port_driver *drv = |
| 913 | to_ps3_vuart_port_driver(_dev->driver); |
| 914 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 915 | down(&vuart_bus_priv.probe_mutex); |
| 916 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 917 | dev_dbg(&dev->core, "%s:%d: %s\n", __func__, __LINE__, |
| 918 | dev->core.bus_id); |
| 919 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 920 | BUG_ON(vuart_bus_priv.use_count < 1); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 921 | |
| 922 | if (drv->remove) |
| 923 | drv->remove(dev); |
| 924 | else |
| 925 | dev_dbg(&dev->core, "%s:%d: %s no remove method\n", __func__, |
| 926 | __LINE__, dev->core.bus_id); |
| 927 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 928 | vuart_bus_priv.devices[dev->priv->port_number] = 0; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 929 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 930 | if (--vuart_bus_priv.use_count == 0) { |
| 931 | BUG(); |
| 932 | free_irq(vuart_bus_priv.virq, &vuart_bus_priv); |
| 933 | ps3_free_vuart_irq(vuart_bus_priv.virq); |
| 934 | vuart_bus_priv.virq = NO_IRQ; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 935 | } |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 936 | |
| 937 | kfree(dev->priv); |
| 938 | dev->priv = NULL; |
| 939 | |
| 940 | up(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 941 | return 0; |
| 942 | } |
| 943 | |
Geert Uytterhoeven | 5b8e8ee | 2007-02-12 00:55:15 -0800 | [diff] [blame] | 944 | static void ps3_vuart_shutdown(struct device *_dev) |
| 945 | { |
| 946 | struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev); |
| 947 | struct ps3_vuart_port_driver *drv = |
| 948 | to_ps3_vuart_port_driver(_dev->driver); |
| 949 | |
| 950 | dev_dbg(&dev->core, "%s:%d: %s\n", __func__, __LINE__, |
| 951 | dev->core.bus_id); |
| 952 | |
| 953 | if (drv->shutdown) |
| 954 | drv->shutdown(dev); |
| 955 | else |
| 956 | dev_dbg(&dev->core, "%s:%d: %s no shutdown method\n", __func__, |
| 957 | __LINE__, dev->core.bus_id); |
| 958 | } |
| 959 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 960 | /** |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 961 | * ps3_vuart_bus - The vuart bus instance. |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 962 | * |
| 963 | * The vuart is managed as a bus that port devices connect to. |
| 964 | */ |
| 965 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 966 | struct bus_type ps3_vuart_bus = { |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 967 | .name = "ps3_vuart", |
| 968 | .match = ps3_vuart_match, |
| 969 | .probe = ps3_vuart_probe, |
| 970 | .remove = ps3_vuart_remove, |
Geert Uytterhoeven | 5b8e8ee | 2007-02-12 00:55:15 -0800 | [diff] [blame] | 971 | .shutdown = ps3_vuart_shutdown, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 972 | }; |
| 973 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 974 | int __init ps3_vuart_bus_init(void) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 975 | { |
| 976 | int result; |
| 977 | |
| 978 | pr_debug("%s:%d:\n", __func__, __LINE__); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 979 | |
| 980 | if (!firmware_has_feature(FW_FEATURE_PS3_LV1)) |
| 981 | return 0; |
| 982 | |
| 983 | init_MUTEX(&vuart_bus_priv.probe_mutex); |
| 984 | result = bus_register(&ps3_vuart_bus); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 985 | BUG_ON(result); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 986 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 987 | return result; |
| 988 | } |
| 989 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 990 | void __exit ps3_vuart_bus_exit(void) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 991 | { |
| 992 | pr_debug("%s:%d:\n", __func__, __LINE__); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 993 | bus_unregister(&ps3_vuart_bus); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 994 | } |
| 995 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 996 | core_initcall(ps3_vuart_bus_init); |
| 997 | module_exit(ps3_vuart_bus_exit); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 998 | |
| 999 | /** |
| 1000 | * ps3_vuart_port_release_device - Remove a vuart port device. |
| 1001 | */ |
| 1002 | |
| 1003 | static void ps3_vuart_port_release_device(struct device *_dev) |
| 1004 | { |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1005 | #if defined(DEBUG) |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 1006 | struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev); |
| 1007 | |
| 1008 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); |
| 1009 | |
| 1010 | BUG_ON(dev->priv && "forgot to free"); |
| 1011 | memset(&dev->core, 0, sizeof(dev->core)); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1012 | #endif |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | /** |
| 1016 | * ps3_vuart_port_device_register - Add a vuart port device. |
| 1017 | */ |
| 1018 | |
| 1019 | int ps3_vuart_port_device_register(struct ps3_vuart_port_device *dev) |
| 1020 | { |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1021 | static unsigned int dev_count = 1; |
| 1022 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 1023 | BUG_ON(dev->priv && "forgot to free"); |
| 1024 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1025 | dev->core.parent = NULL; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 1026 | dev->core.bus = &ps3_vuart_bus; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1027 | dev->core.release = ps3_vuart_port_release_device; |
| 1028 | |
| 1029 | snprintf(dev->core.bus_id, sizeof(dev->core.bus_id), "vuart_%02x", |
| 1030 | dev_count++); |
| 1031 | |
| 1032 | dev_dbg(&dev->core, "%s:%d register\n", __func__, __LINE__); |
| 1033 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 1034 | return device_register(&dev->core); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | EXPORT_SYMBOL_GPL(ps3_vuart_port_device_register); |
| 1038 | |
| 1039 | /** |
| 1040 | * ps3_vuart_port_driver_register - Add a vuart port device driver. |
| 1041 | */ |
| 1042 | |
| 1043 | int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver *drv) |
| 1044 | { |
| 1045 | int result; |
| 1046 | |
| 1047 | pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.name); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 1048 | drv->core.bus = &ps3_vuart_bus; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1049 | result = driver_register(&drv->core); |
| 1050 | return result; |
| 1051 | } |
| 1052 | |
| 1053 | EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register); |
| 1054 | |
| 1055 | /** |
| 1056 | * ps3_vuart_port_driver_unregister - Remove a vuart port device driver. |
| 1057 | */ |
| 1058 | |
| 1059 | void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver *drv) |
| 1060 | { |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame^] | 1061 | pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.name); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1062 | driver_unregister(&drv->core); |
| 1063 | } |
| 1064 | |
| 1065 | EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister); |