1 /* 2 * Copyright (C) 2015 ST Microelectronics 3 * 4 * Author: Lee Jones <lee.jones@linaro.org> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 */ 11 12 #include <linux/debugfs.h> 13 #include <linux/err.h> 14 #include <linux/fs.h> 15 #include <linux/io.h> 16 #include <linux/kernel.h> 17 #include <linux/mailbox_client.h> 18 #include <linux/module.h> 19 #include <linux/of.h> 20 #include <linux/platform_device.h> 21 #include <linux/poll.h> 22 #include <linux/slab.h> 23 #include <linux/uaccess.h> 24 #include <linux/sched/signal.h> 25 26 #define MBOX_MAX_SIG_LEN 8 27 #define MBOX_MAX_MSG_LEN 128 28 #define MBOX_BYTES_PER_LINE 16 29 #define MBOX_HEXDUMP_LINE_LEN ((MBOX_BYTES_PER_LINE * 4) + 2) 30 #define MBOX_HEXDUMP_MAX_LEN (MBOX_HEXDUMP_LINE_LEN * \ 31 (MBOX_MAX_MSG_LEN / MBOX_BYTES_PER_LINE)) 32 33 static struct dentry *root_debugfs_dir; 34 35 struct mbox_test_device { 36 struct device *dev; 37 void __iomem *tx_mmio; 38 void __iomem *rx_mmio; 39 struct mbox_chan *tx_channel; 40 struct mbox_chan *rx_channel; 41 char *rx_buffer; 42 char *signal; 43 char *message; 44 spinlock_t lock; 45 wait_queue_head_t waitq; 46 struct fasync_struct *async_queue; 47 }; 48 49 static ssize_t mbox_test_signal_write(struct file *filp, 50 const char __user *userbuf, 51 size_t count, loff_t *ppos) 52 { 53 struct mbox_test_device *tdev = filp->private_data; 54 55 if (!tdev->tx_channel) { 56 dev_err(tdev->dev, "Channel cannot do Tx\n"); 57 return -EINVAL; 58 } 59 60 if (count > MBOX_MAX_SIG_LEN) { 61 dev_err(tdev->dev, 62 "Signal length %zd greater than max allowed %d\n", 63 count, MBOX_MAX_SIG_LEN); 64 return -EINVAL; 65 } 66 67 /* Only allocate memory if we need to */ 68 if (!tdev->signal) { 69 tdev->signal = kzalloc(MBOX_MAX_SIG_LEN, GFP_KERNEL); 70 if (!tdev->signal) 71 return -ENOMEM; 72 } 73 74 if (copy_from_user(tdev->signal, userbuf, count)) { 75 kfree(tdev->signal); 76 tdev->signal = NULL; 77 return -EFAULT; 78 } 79 80 return count; 81 } 82 83 static const struct file_operations mbox_test_signal_ops = { 84 .write = mbox_test_signal_write, 85 .open = simple_open, 86 .llseek = generic_file_llseek, 87 }; 88 89 static int mbox_test_message_fasync(int fd, struct file *filp, int on) 90 { 91 struct mbox_test_device *tdev = filp->private_data; 92 93 return fasync_helper(fd, filp, on, &tdev->async_queue); 94 } 95 96 static ssize_t mbox_test_message_write(struct file *filp, 97 const char __user *userbuf, 98 size_t count, loff_t *ppos) 99 { 100 struct mbox_test_device *tdev = filp->private_data; 101 void *data; 102 int ret; 103 104 if (!tdev->tx_channel) { 105 dev_err(tdev->dev, "Channel cannot do Tx\n"); 106 return -EINVAL; 107 } 108 109 if (count > MBOX_MAX_MSG_LEN) { 110 dev_err(tdev->dev, 111 "Message length %zd greater than max allowed %d\n", 112 count, MBOX_MAX_MSG_LEN); 113 return -EINVAL; 114 } 115 116 tdev->message = kzalloc(MBOX_MAX_MSG_LEN, GFP_KERNEL); 117 if (!tdev->message) 118 return -ENOMEM; 119 120 ret = copy_from_user(tdev->message, userbuf, count); 121 if (ret) { 122 ret = -EFAULT; 123 goto out; 124 } 125 126 /* 127 * A separate signal is only of use if there is 128 * MMIO to subsequently pass the message through 129 */ 130 if (tdev->tx_mmio && tdev->signal) { 131 print_hex_dump_bytes("Client: Sending: Signal: ", DUMP_PREFIX_ADDRESS, 132 tdev->signal, MBOX_MAX_SIG_LEN); 133 134 data = tdev->signal; 135 } else 136 data = tdev->message; 137 138 print_hex_dump_bytes("Client: Sending: Message: ", DUMP_PREFIX_ADDRESS, 139 tdev->message, MBOX_MAX_MSG_LEN); 140 141 ret = mbox_send_message(tdev->tx_channel, data); 142 if (ret < 0) 143 dev_err(tdev->dev, "Failed to send message via mailbox\n"); 144 145 out: 146 kfree(tdev->signal); 147 kfree(tdev->message); 148 tdev->signal = NULL; 149 150 return ret < 0 ? ret : count; 151 } 152 153 static bool mbox_test_message_data_ready(struct mbox_test_device *tdev) 154 { 155 unsigned char data; 156 unsigned long flags; 157 158 spin_lock_irqsave(&tdev->lock, flags); 159 data = tdev->rx_buffer[0]; 160 spin_unlock_irqrestore(&tdev->lock, flags); 161 162 if (data != '\0') 163 return true; 164 return false; 165 } 166 167 static ssize_t mbox_test_message_read(struct file *filp, char __user *userbuf, 168 size_t count, loff_t *ppos) 169 { 170 struct mbox_test_device *tdev = filp->private_data; 171 unsigned long flags; 172 char *touser, *ptr; 173 int l = 0; 174 int ret; 175 176 DECLARE_WAITQUEUE(wait, current); 177 178 touser = kzalloc(MBOX_HEXDUMP_MAX_LEN + 1, GFP_KERNEL); 179 if (!touser) 180 return -ENOMEM; 181 182 if (!tdev->rx_channel) { 183 ret = snprintf(touser, 20, "<NO RX CAPABILITY>\n"); 184 ret = simple_read_from_buffer(userbuf, count, ppos, 185 touser, ret); 186 goto kfree_err; 187 } 188 189 add_wait_queue(&tdev->waitq, &wait); 190 191 do { 192 __set_current_state(TASK_INTERRUPTIBLE); 193 194 if (mbox_test_message_data_ready(tdev)) 195 break; 196 197 if (filp->f_flags & O_NONBLOCK) { 198 ret = -EAGAIN; 199 goto waitq_err; 200 } 201 202 if (signal_pending(current)) { 203 ret = -ERESTARTSYS; 204 goto waitq_err; 205 } 206 schedule(); 207 208 } while (1); 209 210 spin_lock_irqsave(&tdev->lock, flags); 211 212 ptr = tdev->rx_buffer; 213 while (l < MBOX_HEXDUMP_MAX_LEN) { 214 hex_dump_to_buffer(ptr, 215 MBOX_BYTES_PER_LINE, 216 MBOX_BYTES_PER_LINE, 1, touser + l, 217 MBOX_HEXDUMP_LINE_LEN, true); 218 219 ptr += MBOX_BYTES_PER_LINE; 220 l += MBOX_HEXDUMP_LINE_LEN; 221 *(touser + (l - 1)) = '\n'; 222 } 223 *(touser + l) = '\0'; 224 225 memset(tdev->rx_buffer, 0, MBOX_MAX_MSG_LEN); 226 227 spin_unlock_irqrestore(&tdev->lock, flags); 228 229 ret = simple_read_from_buffer(userbuf, count, ppos, touser, MBOX_HEXDUMP_MAX_LEN); 230 waitq_err: 231 __set_current_state(TASK_RUNNING); 232 remove_wait_queue(&tdev->waitq, &wait); 233 kfree_err: 234 kfree(touser); 235 return ret; 236 } 237 238 static unsigned int 239 mbox_test_message_poll(struct file *filp, struct poll_table_struct *wait) 240 { 241 struct mbox_test_device *tdev = filp->private_data; 242 243 poll_wait(filp, &tdev->waitq, wait); 244 245 if (mbox_test_message_data_ready(tdev)) 246 return POLLIN | POLLRDNORM; 247 return 0; 248 } 249 250 static const struct file_operations mbox_test_message_ops = { 251 .write = mbox_test_message_write, 252 .read = mbox_test_message_read, 253 .fasync = mbox_test_message_fasync, 254 .poll = mbox_test_message_poll, 255 .open = simple_open, 256 .llseek = generic_file_llseek, 257 }; 258 259 static int mbox_test_add_debugfs(struct platform_device *pdev, 260 struct mbox_test_device *tdev) 261 { 262 if (!debugfs_initialized()) 263 return 0; 264 265 root_debugfs_dir = debugfs_create_dir("mailbox", NULL); 266 if (!root_debugfs_dir) { 267 dev_err(&pdev->dev, "Failed to create Mailbox debugfs\n"); 268 return -EINVAL; 269 } 270 271 debugfs_create_file("message", 0600, root_debugfs_dir, 272 tdev, &mbox_test_message_ops); 273 274 debugfs_create_file("signal", 0200, root_debugfs_dir, 275 tdev, &mbox_test_signal_ops); 276 277 return 0; 278 } 279 280 static void mbox_test_receive_message(struct mbox_client *client, void *message) 281 { 282 struct mbox_test_device *tdev = dev_get_drvdata(client->dev); 283 unsigned long flags; 284 285 spin_lock_irqsave(&tdev->lock, flags); 286 if (tdev->rx_mmio) { 287 memcpy_fromio(tdev->rx_buffer, tdev->rx_mmio, MBOX_MAX_MSG_LEN); 288 print_hex_dump_bytes("Client: Received [MMIO]: ", DUMP_PREFIX_ADDRESS, 289 tdev->rx_buffer, MBOX_MAX_MSG_LEN); 290 } else if (message) { 291 print_hex_dump_bytes("Client: Received [API]: ", DUMP_PREFIX_ADDRESS, 292 message, MBOX_MAX_MSG_LEN); 293 memcpy(tdev->rx_buffer, message, MBOX_MAX_MSG_LEN); 294 } 295 spin_unlock_irqrestore(&tdev->lock, flags); 296 297 wake_up_interruptible(&tdev->waitq); 298 299 kill_fasync(&tdev->async_queue, SIGIO, POLL_IN); 300 } 301 302 static void mbox_test_prepare_message(struct mbox_client *client, void *message) 303 { 304 struct mbox_test_device *tdev = dev_get_drvdata(client->dev); 305 306 if (tdev->tx_mmio) { 307 if (tdev->signal) 308 memcpy_toio(tdev->tx_mmio, tdev->message, MBOX_MAX_MSG_LEN); 309 else 310 memcpy_toio(tdev->tx_mmio, message, MBOX_MAX_MSG_LEN); 311 } 312 } 313 314 static void mbox_test_message_sent(struct mbox_client *client, 315 void *message, int r) 316 { 317 if (r) 318 dev_warn(client->dev, 319 "Client: Message could not be sent: %d\n", r); 320 else 321 dev_info(client->dev, 322 "Client: Message sent\n"); 323 } 324 325 static struct mbox_chan * 326 mbox_test_request_channel(struct platform_device *pdev, const char *name) 327 { 328 struct mbox_client *client; 329 struct mbox_chan *channel; 330 331 client = devm_kzalloc(&pdev->dev, sizeof(*client), GFP_KERNEL); 332 if (!client) 333 return ERR_PTR(-ENOMEM); 334 335 client->dev = &pdev->dev; 336 client->rx_callback = mbox_test_receive_message; 337 client->tx_prepare = mbox_test_prepare_message; 338 client->tx_done = mbox_test_message_sent; 339 client->tx_block = true; 340 client->knows_txdone = false; 341 client->tx_tout = 500; 342 343 channel = mbox_request_channel_byname(client, name); 344 if (IS_ERR(channel)) { 345 dev_warn(&pdev->dev, "Failed to request %s channel\n", name); 346 return NULL; 347 } 348 349 return channel; 350 } 351 352 static int mbox_test_probe(struct platform_device *pdev) 353 { 354 struct mbox_test_device *tdev; 355 struct resource *res; 356 resource_size_t size; 357 int ret; 358 359 tdev = devm_kzalloc(&pdev->dev, sizeof(*tdev), GFP_KERNEL); 360 if (!tdev) 361 return -ENOMEM; 362 363 /* It's okay for MMIO to be NULL */ 364 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 365 size = resource_size(res); 366 tdev->tx_mmio = devm_ioremap_resource(&pdev->dev, res); 367 if (PTR_ERR(tdev->tx_mmio) == -EBUSY) 368 /* if reserved area in SRAM, try just ioremap */ 369 tdev->tx_mmio = devm_ioremap(&pdev->dev, res->start, size); 370 else if (IS_ERR(tdev->tx_mmio)) 371 tdev->tx_mmio = NULL; 372 373 /* If specified, second reg entry is Rx MMIO */ 374 res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 375 size = resource_size(res); 376 tdev->rx_mmio = devm_ioremap_resource(&pdev->dev, res); 377 if (PTR_ERR(tdev->rx_mmio) == -EBUSY) 378 tdev->rx_mmio = devm_ioremap(&pdev->dev, res->start, size); 379 else if (IS_ERR(tdev->rx_mmio)) 380 tdev->rx_mmio = tdev->tx_mmio; 381 382 tdev->tx_channel = mbox_test_request_channel(pdev, "tx"); 383 tdev->rx_channel = mbox_test_request_channel(pdev, "rx"); 384 385 if (!tdev->tx_channel && !tdev->rx_channel) 386 return -EPROBE_DEFER; 387 388 /* If Rx is not specified but has Rx MMIO, then Rx = Tx */ 389 if (!tdev->rx_channel && (tdev->rx_mmio != tdev->tx_mmio)) 390 tdev->rx_channel = tdev->tx_channel; 391 392 tdev->dev = &pdev->dev; 393 platform_set_drvdata(pdev, tdev); 394 395 spin_lock_init(&tdev->lock); 396 397 if (tdev->rx_channel) { 398 tdev->rx_buffer = devm_kzalloc(&pdev->dev, 399 MBOX_MAX_MSG_LEN, GFP_KERNEL); 400 if (!tdev->rx_buffer) 401 return -ENOMEM; 402 } 403 404 ret = mbox_test_add_debugfs(pdev, tdev); 405 if (ret) 406 return ret; 407 408 init_waitqueue_head(&tdev->waitq); 409 dev_info(&pdev->dev, "Successfully registered\n"); 410 411 return 0; 412 } 413 414 static int mbox_test_remove(struct platform_device *pdev) 415 { 416 struct mbox_test_device *tdev = platform_get_drvdata(pdev); 417 418 debugfs_remove_recursive(root_debugfs_dir); 419 420 if (tdev->tx_channel) 421 mbox_free_channel(tdev->tx_channel); 422 if (tdev->rx_channel) 423 mbox_free_channel(tdev->rx_channel); 424 425 return 0; 426 } 427 428 static const struct of_device_id mbox_test_match[] = { 429 { .compatible = "mailbox-test" }, 430 {}, 431 }; 432 MODULE_DEVICE_TABLE(of, mbox_test_match); 433 434 static struct platform_driver mbox_test_driver = { 435 .driver = { 436 .name = "mailbox_test", 437 .of_match_table = mbox_test_match, 438 }, 439 .probe = mbox_test_probe, 440 .remove = mbox_test_remove, 441 }; 442 module_platform_driver(mbox_test_driver); 443 444 MODULE_DESCRIPTION("Generic Mailbox Testing Facility"); 445 MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org"); 446 MODULE_LICENSE("GPL v2"); 447