J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * pti.c - PTI driver for cJTAG data extration |
| 3 | * |
| 4 | * Copyright (C) Intel 2010 |
| 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 version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 16 | * |
| 17 | * The PTI (Parallel Trace Interface) driver directs trace data routed from |
| 18 | * various parts in the system out through the Intel Penwell PTI port and |
| 19 | * out of the mobile device for analysis with a debugging tool |
| 20 | * (Lauterbach, Fido). This is part of a solution for the MIPI P1149.7, |
| 21 | * compact JTAG, standard. |
| 22 | */ |
| 23 | |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/sched.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/console.h> |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/tty.h> |
| 31 | #include <linux/tty_driver.h> |
| 32 | #include <linux/pci.h> |
| 33 | #include <linux/mutex.h> |
| 34 | #include <linux/miscdevice.h> |
| 35 | #include <linux/pti.h> |
Sergei Trofimovich | 06ed462 | 2011-08-25 15:59:01 -0700 | [diff] [blame] | 36 | #include <linux/slab.h> |
| 37 | #include <linux/uaccess.h> |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 38 | |
| 39 | #define DRIVERNAME "pti" |
| 40 | #define PCINAME "pciPTI" |
| 41 | #define TTYNAME "ttyPTI" |
| 42 | #define CHARNAME "pti" |
| 43 | #define PTITTY_MINOR_START 0 |
| 44 | #define PTITTY_MINOR_NUM 2 |
| 45 | #define MAX_APP_IDS 16 /* 128 channel ids / u8 bit size */ |
| 46 | #define MAX_OS_IDS 16 /* 128 channel ids / u8 bit size */ |
| 47 | #define MAX_MODEM_IDS 16 /* 128 channel ids / u8 bit size */ |
| 48 | #define MODEM_BASE_ID 71 /* modem master ID address */ |
| 49 | #define CONTROL_ID 72 /* control master ID address */ |
| 50 | #define CONSOLE_ID 73 /* console master ID address */ |
| 51 | #define OS_BASE_ID 74 /* base OS master ID address */ |
| 52 | #define APP_BASE_ID 80 /* base App master ID address */ |
| 53 | #define CONTROL_FRAME_LEN 32 /* PTI control frame maximum size */ |
| 54 | #define USER_COPY_SIZE 8192 /* 8Kb buffer for user space copy */ |
| 55 | #define APERTURE_14 0x3800000 /* offset to first OS write addr */ |
| 56 | #define APERTURE_LEN 0x400000 /* address length */ |
| 57 | |
| 58 | struct pti_tty { |
| 59 | struct pti_masterchannel *mc; |
| 60 | }; |
| 61 | |
| 62 | struct pti_dev { |
| 63 | struct tty_port port; |
| 64 | unsigned long pti_addr; |
| 65 | unsigned long aperture_base; |
| 66 | void __iomem *pti_ioaddr; |
| 67 | u8 ia_app[MAX_APP_IDS]; |
| 68 | u8 ia_os[MAX_OS_IDS]; |
| 69 | u8 ia_modem[MAX_MODEM_IDS]; |
| 70 | }; |
| 71 | |
| 72 | /* |
| 73 | * This protects access to ia_app, ia_os, and ia_modem, |
| 74 | * which keeps track of channels allocated in |
| 75 | * an aperture write id. |
| 76 | */ |
| 77 | static DEFINE_MUTEX(alloclock); |
| 78 | |
| 79 | static struct pci_device_id pci_ids[] __devinitconst = { |
| 80 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x82B)}, |
| 81 | {0} |
| 82 | }; |
| 83 | |
| 84 | static struct tty_driver *pti_tty_driver; |
| 85 | static struct pti_dev *drv_data; |
| 86 | |
| 87 | static unsigned int pti_console_channel; |
| 88 | static unsigned int pti_control_channel; |
| 89 | |
| 90 | /** |
| 91 | * pti_write_to_aperture()- The private write function to PTI HW. |
| 92 | * |
| 93 | * @mc: The 'aperture'. It's part of a write address that holds |
| 94 | * a master and channel ID. |
| 95 | * @buf: Data being written to the HW that will ultimately be seen |
| 96 | * in a debugging tool (Fido, Lauterbach). |
| 97 | * @len: Size of buffer. |
| 98 | * |
| 99 | * Since each aperture is specified by a unique |
| 100 | * master/channel ID, no two processes will be writing |
| 101 | * to the same aperture at the same time so no lock is required. The |
| 102 | * PTI-Output agent will send these out in the order that they arrived, and |
| 103 | * thus, it will intermix these messages. The debug tool can then later |
| 104 | * regroup the appropriate message segments together reconstituting each |
| 105 | * message. |
| 106 | */ |
| 107 | static void pti_write_to_aperture(struct pti_masterchannel *mc, |
| 108 | u8 *buf, |
| 109 | int len) |
| 110 | { |
| 111 | int dwordcnt; |
| 112 | int final; |
| 113 | int i; |
| 114 | u32 ptiword; |
| 115 | u32 __iomem *aperture; |
| 116 | u8 *p = buf; |
| 117 | |
| 118 | /* |
| 119 | * calculate the aperture offset from the base using the master and |
| 120 | * channel id's. |
| 121 | */ |
| 122 | aperture = drv_data->pti_ioaddr + (mc->master << 15) |
| 123 | + (mc->channel << 8); |
| 124 | |
| 125 | dwordcnt = len >> 2; |
| 126 | final = len - (dwordcnt << 2); /* final = trailing bytes */ |
| 127 | if (final == 0 && dwordcnt != 0) { /* always need a final dword */ |
| 128 | final += 4; |
| 129 | dwordcnt--; |
| 130 | } |
| 131 | |
| 132 | for (i = 0; i < dwordcnt; i++) { |
| 133 | ptiword = be32_to_cpu(*(u32 *)p); |
| 134 | p += 4; |
| 135 | iowrite32(ptiword, aperture); |
| 136 | } |
| 137 | |
| 138 | aperture += PTI_LASTDWORD_DTS; /* adding DTS signals that is EOM */ |
| 139 | |
| 140 | ptiword = 0; |
| 141 | for (i = 0; i < final; i++) |
| 142 | ptiword |= *p++ << (24-(8*i)); |
| 143 | |
| 144 | iowrite32(ptiword, aperture); |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * pti_control_frame_built_and_sent()- control frame build and send function. |
| 150 | * |
J Freyensee | 8168e9c | 2011-06-17 15:09:53 -0700 | [diff] [blame] | 151 | * @mc: The master / channel structure on which the function |
| 152 | * built a control frame. |
| 153 | * @thread_name: The thread name associated with the master / channel or |
| 154 | * 'NULL' if using the 'current' global variable. |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 155 | * |
| 156 | * To be able to post process the PTI contents on host side, a control frame |
| 157 | * is added before sending any PTI content. So the host side knows on |
| 158 | * each PTI frame the name of the thread using a dedicated master / channel. |
J Freyensee | 8168e9c | 2011-06-17 15:09:53 -0700 | [diff] [blame] | 159 | * The thread name is retrieved from 'current' global variable if 'thread_name' |
| 160 | * is 'NULL', else it is retrieved from 'thread_name' parameter. |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 161 | * This function builds this frame and sends it to a master ID CONTROL_ID. |
| 162 | * The overhead is only 32 bytes since the driver only writes to HW |
| 163 | * in 32 byte chunks. |
| 164 | */ |
J Freyensee | 8168e9c | 2011-06-17 15:09:53 -0700 | [diff] [blame] | 165 | static void pti_control_frame_built_and_sent(struct pti_masterchannel *mc, |
| 166 | const char *thread_name) |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 167 | { |
| 168 | struct pti_masterchannel mccontrol = {.master = CONTROL_ID, |
| 169 | .channel = 0}; |
J Freyensee | 8168e9c | 2011-06-17 15:09:53 -0700 | [diff] [blame] | 170 | const char *thread_name_p; |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 171 | const char *control_format = "%3d %3d %s"; |
| 172 | u8 control_frame[CONTROL_FRAME_LEN]; |
| 173 | |
J Freyensee | 8168e9c | 2011-06-17 15:09:53 -0700 | [diff] [blame] | 174 | if (!thread_name) { |
| 175 | /* |
| 176 | * Since we access the comm member in current's task_struct, |
| 177 | * we only need to be as large as what 'comm' in that |
| 178 | * structure is. |
| 179 | */ |
| 180 | char comm[TASK_COMM_LEN]; |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 181 | |
J Freyensee | 8168e9c | 2011-06-17 15:09:53 -0700 | [diff] [blame] | 182 | if (!in_interrupt()) |
| 183 | get_task_comm(comm, current); |
| 184 | else |
| 185 | strncpy(comm, "Interrupt", TASK_COMM_LEN); |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 186 | |
J Freyensee | 8168e9c | 2011-06-17 15:09:53 -0700 | [diff] [blame] | 187 | /* Absolutely ensure our buffer is zero terminated. */ |
| 188 | comm[TASK_COMM_LEN-1] = 0; |
| 189 | thread_name_p = comm; |
| 190 | } else { |
| 191 | thread_name_p = thread_name; |
| 192 | } |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 193 | |
| 194 | mccontrol.channel = pti_control_channel; |
| 195 | pti_control_channel = (pti_control_channel + 1) & 0x7f; |
| 196 | |
| 197 | snprintf(control_frame, CONTROL_FRAME_LEN, control_format, mc->master, |
J Freyensee | 8168e9c | 2011-06-17 15:09:53 -0700 | [diff] [blame] | 198 | mc->channel, thread_name_p); |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 199 | pti_write_to_aperture(&mccontrol, control_frame, strlen(control_frame)); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * pti_write_full_frame_to_aperture()- high level function to |
| 204 | * write to PTI. |
| 205 | * |
| 206 | * @mc: The 'aperture'. It's part of a write address that holds |
| 207 | * a master and channel ID. |
| 208 | * @buf: Data being written to the HW that will ultimately be seen |
| 209 | * in a debugging tool (Fido, Lauterbach). |
| 210 | * @len: Size of buffer. |
| 211 | * |
| 212 | * All threads sending data (either console, user space application, ...) |
| 213 | * are calling the high level function to write to PTI meaning that it is |
| 214 | * possible to add a control frame before sending the content. |
| 215 | */ |
| 216 | static void pti_write_full_frame_to_aperture(struct pti_masterchannel *mc, |
| 217 | const unsigned char *buf, |
| 218 | int len) |
| 219 | { |
J Freyensee | 8168e9c | 2011-06-17 15:09:53 -0700 | [diff] [blame] | 220 | pti_control_frame_built_and_sent(mc, NULL); |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 221 | pti_write_to_aperture(mc, (u8 *)buf, len); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * get_id()- Allocate a master and channel ID. |
| 226 | * |
J Freyensee | 8168e9c | 2011-06-17 15:09:53 -0700 | [diff] [blame] | 227 | * @id_array: an array of bits representing what channel |
| 228 | * id's are allocated for writing. |
| 229 | * @max_ids: The max amount of available write IDs to use. |
| 230 | * @base_id: The starting SW channel ID, based on the Intel |
| 231 | * PTI arch. |
| 232 | * @thread_name: The thread name associated with the master / channel or |
| 233 | * 'NULL' if using the 'current' global variable. |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 234 | * |
| 235 | * Returns: |
| 236 | * pti_masterchannel struct with master, channel ID address |
| 237 | * 0 for error |
| 238 | * |
| 239 | * Each bit in the arrays ia_app and ia_os correspond to a master and |
| 240 | * channel id. The bit is one if the id is taken and 0 if free. For |
| 241 | * every master there are 128 channel id's. |
| 242 | */ |
J Freyensee | 8168e9c | 2011-06-17 15:09:53 -0700 | [diff] [blame] | 243 | static struct pti_masterchannel *get_id(u8 *id_array, |
| 244 | int max_ids, |
| 245 | int base_id, |
| 246 | const char *thread_name) |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 247 | { |
| 248 | struct pti_masterchannel *mc; |
| 249 | int i, j, mask; |
| 250 | |
| 251 | mc = kmalloc(sizeof(struct pti_masterchannel), GFP_KERNEL); |
| 252 | if (mc == NULL) |
| 253 | return NULL; |
| 254 | |
| 255 | /* look for a byte with a free bit */ |
| 256 | for (i = 0; i < max_ids; i++) |
| 257 | if (id_array[i] != 0xff) |
| 258 | break; |
| 259 | if (i == max_ids) { |
| 260 | kfree(mc); |
| 261 | return NULL; |
| 262 | } |
| 263 | /* find the bit in the 128 possible channel opportunities */ |
| 264 | mask = 0x80; |
| 265 | for (j = 0; j < 8; j++) { |
| 266 | if ((id_array[i] & mask) == 0) |
| 267 | break; |
| 268 | mask >>= 1; |
| 269 | } |
| 270 | |
| 271 | /* grab it */ |
| 272 | id_array[i] |= mask; |
| 273 | mc->master = base_id; |
| 274 | mc->channel = ((i & 0xf)<<3) + j; |
| 275 | /* write new master Id / channel Id allocation to channel control */ |
J Freyensee | 8168e9c | 2011-06-17 15:09:53 -0700 | [diff] [blame] | 276 | pti_control_frame_built_and_sent(mc, thread_name); |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 277 | return mc; |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * The following three functions: |
| 282 | * pti_request_mastercahannel(), mipi_release_masterchannel() |
| 283 | * and pti_writedata() are an API for other kernel drivers to |
| 284 | * access PTI. |
| 285 | */ |
| 286 | |
| 287 | /** |
| 288 | * pti_request_masterchannel()- Kernel API function used to allocate |
| 289 | * a master, channel ID address |
| 290 | * to write to PTI HW. |
| 291 | * |
J Freyensee | 8168e9c | 2011-06-17 15:09:53 -0700 | [diff] [blame] | 292 | * @type: 0- request Application master, channel aperture ID |
| 293 | * write address. |
| 294 | * 1- request OS master, channel aperture ID write |
| 295 | * address. |
| 296 | * 2- request Modem master, channel aperture ID |
| 297 | * write address. |
| 298 | * Other values, error. |
| 299 | * @thread_name: The thread name associated with the master / channel or |
| 300 | * 'NULL' if using the 'current' global variable. |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 301 | * |
| 302 | * Returns: |
| 303 | * pti_masterchannel struct |
| 304 | * 0 for error |
| 305 | */ |
J Freyensee | 8168e9c | 2011-06-17 15:09:53 -0700 | [diff] [blame] | 306 | struct pti_masterchannel *pti_request_masterchannel(u8 type, |
| 307 | const char *thread_name) |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 308 | { |
| 309 | struct pti_masterchannel *mc; |
| 310 | |
| 311 | mutex_lock(&alloclock); |
| 312 | |
| 313 | switch (type) { |
| 314 | |
| 315 | case 0: |
J Freyensee | 8168e9c | 2011-06-17 15:09:53 -0700 | [diff] [blame] | 316 | mc = get_id(drv_data->ia_app, MAX_APP_IDS, |
| 317 | APP_BASE_ID, thread_name); |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 318 | break; |
| 319 | |
| 320 | case 1: |
J Freyensee | 8168e9c | 2011-06-17 15:09:53 -0700 | [diff] [blame] | 321 | mc = get_id(drv_data->ia_os, MAX_OS_IDS, |
| 322 | OS_BASE_ID, thread_name); |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 323 | break; |
| 324 | |
| 325 | case 2: |
J Freyensee | 8168e9c | 2011-06-17 15:09:53 -0700 | [diff] [blame] | 326 | mc = get_id(drv_data->ia_modem, MAX_MODEM_IDS, |
| 327 | MODEM_BASE_ID, thread_name); |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 328 | break; |
| 329 | default: |
| 330 | mc = NULL; |
| 331 | } |
| 332 | |
| 333 | mutex_unlock(&alloclock); |
| 334 | return mc; |
| 335 | } |
| 336 | EXPORT_SYMBOL_GPL(pti_request_masterchannel); |
| 337 | |
| 338 | /** |
| 339 | * pti_release_masterchannel()- Kernel API function used to release |
| 340 | * a master, channel ID address |
| 341 | * used to write to PTI HW. |
| 342 | * |
J Freyensee | 29021bcc | 2011-05-25 14:38:18 -0700 | [diff] [blame] | 343 | * @mc: master, channel apeture ID address to be released. This |
| 344 | * will de-allocate the structure via kfree(). |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 345 | */ |
| 346 | void pti_release_masterchannel(struct pti_masterchannel *mc) |
| 347 | { |
| 348 | u8 master, channel, i; |
| 349 | |
| 350 | mutex_lock(&alloclock); |
| 351 | |
| 352 | if (mc) { |
| 353 | master = mc->master; |
| 354 | channel = mc->channel; |
| 355 | |
| 356 | if (master == APP_BASE_ID) { |
| 357 | i = channel >> 3; |
| 358 | drv_data->ia_app[i] &= ~(0x80>>(channel & 0x7)); |
| 359 | } else if (master == OS_BASE_ID) { |
| 360 | i = channel >> 3; |
| 361 | drv_data->ia_os[i] &= ~(0x80>>(channel & 0x7)); |
| 362 | } else { |
| 363 | i = channel >> 3; |
| 364 | drv_data->ia_modem[i] &= ~(0x80>>(channel & 0x7)); |
| 365 | } |
| 366 | |
| 367 | kfree(mc); |
| 368 | } |
| 369 | |
| 370 | mutex_unlock(&alloclock); |
| 371 | } |
| 372 | EXPORT_SYMBOL_GPL(pti_release_masterchannel); |
| 373 | |
| 374 | /** |
| 375 | * pti_writedata()- Kernel API function used to write trace |
| 376 | * debugging data to PTI HW. |
| 377 | * |
| 378 | * @mc: Master, channel aperture ID address to write to. |
| 379 | * Null value will return with no write occurring. |
| 380 | * @buf: Trace debuging data to write to the PTI HW. |
| 381 | * Null value will return with no write occurring. |
| 382 | * @count: Size of buf. Value of 0 or a negative number will |
| 383 | * return with no write occuring. |
| 384 | */ |
| 385 | void pti_writedata(struct pti_masterchannel *mc, u8 *buf, int count) |
| 386 | { |
| 387 | /* |
| 388 | * since this function is exported, this is treated like an |
| 389 | * API function, thus, all parameters should |
| 390 | * be checked for validity. |
| 391 | */ |
| 392 | if ((mc != NULL) && (buf != NULL) && (count > 0)) |
| 393 | pti_write_to_aperture(mc, buf, count); |
| 394 | return; |
| 395 | } |
| 396 | EXPORT_SYMBOL_GPL(pti_writedata); |
| 397 | |
| 398 | /** |
| 399 | * pti_pci_remove()- Driver exit method to remove PTI from |
| 400 | * PCI bus. |
| 401 | * @pdev: variable containing pci info of PTI. |
| 402 | */ |
| 403 | static void __devexit pti_pci_remove(struct pci_dev *pdev) |
| 404 | { |
| 405 | struct pti_dev *drv_data; |
| 406 | |
| 407 | drv_data = pci_get_drvdata(pdev); |
| 408 | if (drv_data != NULL) { |
| 409 | pci_iounmap(pdev, drv_data->pti_ioaddr); |
| 410 | pci_set_drvdata(pdev, NULL); |
| 411 | kfree(drv_data); |
| 412 | pci_release_region(pdev, 1); |
| 413 | pci_disable_device(pdev); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | /* |
| 418 | * for the tty_driver_*() basic function descriptions, see tty_driver.h. |
| 419 | * Specific header comments made for PTI-related specifics. |
| 420 | */ |
| 421 | |
| 422 | /** |
| 423 | * pti_tty_driver_open()- Open an Application master, channel aperture |
| 424 | * ID to the PTI device via tty device. |
| 425 | * |
| 426 | * @tty: tty interface. |
| 427 | * @filp: filp interface pased to tty_port_open() call. |
| 428 | * |
| 429 | * Returns: |
| 430 | * int, 0 for success |
| 431 | * otherwise, fail value |
| 432 | * |
| 433 | * The main purpose of using the tty device interface is for |
| 434 | * each tty port to have a unique PTI write aperture. In an |
| 435 | * example use case, ttyPTI0 gets syslogd and an APP aperture |
| 436 | * ID and ttyPTI1 is where the n_tracesink ldisc hooks to route |
| 437 | * modem messages into PTI. Modem trace data does not have to |
| 438 | * go to ttyPTI1, but ttyPTI0 and ttyPTI1 do need to be distinct |
| 439 | * master IDs. These messages go through the PTI HW and out of |
| 440 | * the handheld platform and to the Fido/Lauterbach device. |
| 441 | */ |
| 442 | static int pti_tty_driver_open(struct tty_struct *tty, struct file *filp) |
| 443 | { |
| 444 | /* |
| 445 | * we actually want to allocate a new channel per open, per |
| 446 | * system arch. HW gives more than plenty channels for a single |
| 447 | * system task to have its own channel to write trace data. This |
| 448 | * also removes a locking requirement for the actual write |
| 449 | * procedure. |
| 450 | */ |
| 451 | return tty_port_open(&drv_data->port, tty, filp); |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * pti_tty_driver_close()- close tty device and release Application |
| 456 | * master, channel aperture ID to the PTI device via tty device. |
| 457 | * |
| 458 | * @tty: tty interface. |
| 459 | * @filp: filp interface pased to tty_port_close() call. |
| 460 | * |
| 461 | * The main purpose of using the tty device interface is to route |
| 462 | * syslog daemon messages to the PTI HW and out of the handheld platform |
| 463 | * and to the Fido/Lauterbach device. |
| 464 | */ |
| 465 | static void pti_tty_driver_close(struct tty_struct *tty, struct file *filp) |
| 466 | { |
| 467 | tty_port_close(&drv_data->port, tty, filp); |
| 468 | } |
| 469 | |
| 470 | /** |
J Freyensee | e556b81 | 2011-05-25 14:50:26 -0700 | [diff] [blame] | 471 | * pti_tty_install()- Used to set up specific master-channels |
| 472 | * to tty ports for organizational purposes when |
| 473 | * tracing viewed from debuging tools. |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 474 | * |
| 475 | * @driver: tty driver information. |
| 476 | * @tty: tty struct containing pti information. |
| 477 | * |
| 478 | * Returns: |
| 479 | * 0 for success |
| 480 | * otherwise, error |
| 481 | */ |
| 482 | static int pti_tty_install(struct tty_driver *driver, struct tty_struct *tty) |
| 483 | { |
| 484 | int idx = tty->index; |
| 485 | struct pti_tty *pti_tty_data; |
| 486 | int ret = tty_init_termios(tty); |
| 487 | |
| 488 | if (ret == 0) { |
| 489 | tty_driver_kref_get(driver); |
| 490 | tty->count++; |
| 491 | driver->ttys[idx] = tty; |
| 492 | |
| 493 | pti_tty_data = kmalloc(sizeof(struct pti_tty), GFP_KERNEL); |
| 494 | if (pti_tty_data == NULL) |
| 495 | return -ENOMEM; |
| 496 | |
| 497 | if (idx == PTITTY_MINOR_START) |
J Freyensee | 8168e9c | 2011-06-17 15:09:53 -0700 | [diff] [blame] | 498 | pti_tty_data->mc = pti_request_masterchannel(0, NULL); |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 499 | else |
J Freyensee | 8168e9c | 2011-06-17 15:09:53 -0700 | [diff] [blame] | 500 | pti_tty_data->mc = pti_request_masterchannel(2, NULL); |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 501 | |
J Freyensee | 1dae42b | 2011-05-25 14:45:40 -0700 | [diff] [blame] | 502 | if (pti_tty_data->mc == NULL) { |
| 503 | kfree(pti_tty_data); |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 504 | return -ENXIO; |
J Freyensee | 1dae42b | 2011-05-25 14:45:40 -0700 | [diff] [blame] | 505 | } |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 506 | tty->driver_data = pti_tty_data; |
| 507 | } |
| 508 | |
| 509 | return ret; |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * pti_tty_cleanup()- Used to de-allocate master-channel resources |
| 514 | * tied to tty's of this driver. |
| 515 | * |
| 516 | * @tty: tty struct containing pti information. |
| 517 | */ |
| 518 | static void pti_tty_cleanup(struct tty_struct *tty) |
| 519 | { |
| 520 | struct pti_tty *pti_tty_data = tty->driver_data; |
| 521 | if (pti_tty_data == NULL) |
| 522 | return; |
| 523 | pti_release_masterchannel(pti_tty_data->mc); |
J Freyensee | 1312ba4 | 2011-05-25 14:56:43 -0700 | [diff] [blame] | 524 | kfree(pti_tty_data); |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 525 | tty->driver_data = NULL; |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * pti_tty_driver_write()- Write trace debugging data through the char |
| 530 | * interface to the PTI HW. Part of the misc device implementation. |
| 531 | * |
| 532 | * @filp: Contains private data which is used to obtain |
| 533 | * master, channel write ID. |
| 534 | * @data: trace data to be written. |
| 535 | * @len: # of byte to write. |
| 536 | * |
| 537 | * Returns: |
| 538 | * int, # of bytes written |
| 539 | * otherwise, error |
| 540 | */ |
| 541 | static int pti_tty_driver_write(struct tty_struct *tty, |
| 542 | const unsigned char *buf, int len) |
| 543 | { |
| 544 | struct pti_tty *pti_tty_data = tty->driver_data; |
| 545 | if ((pti_tty_data != NULL) && (pti_tty_data->mc != NULL)) { |
| 546 | pti_write_to_aperture(pti_tty_data->mc, (u8 *)buf, len); |
| 547 | return len; |
| 548 | } |
| 549 | /* |
| 550 | * we can't write to the pti hardware if the private driver_data |
| 551 | * and the mc address is not there. |
| 552 | */ |
| 553 | else |
| 554 | return -EFAULT; |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * pti_tty_write_room()- Always returns 2048. |
| 559 | * |
| 560 | * @tty: contains tty info of the pti driver. |
| 561 | */ |
| 562 | static int pti_tty_write_room(struct tty_struct *tty) |
| 563 | { |
| 564 | return 2048; |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * pti_char_open()- Open an Application master, channel aperture |
| 569 | * ID to the PTI device. Part of the misc device implementation. |
| 570 | * |
| 571 | * @inode: not used. |
| 572 | * @filp: Output- will have a masterchannel struct set containing |
| 573 | * the allocated application PTI aperture write address. |
| 574 | * |
| 575 | * Returns: |
| 576 | * int, 0 for success |
| 577 | * otherwise, a fail value |
| 578 | */ |
| 579 | static int pti_char_open(struct inode *inode, struct file *filp) |
| 580 | { |
| 581 | struct pti_masterchannel *mc; |
| 582 | |
| 583 | /* |
| 584 | * We really do want to fail immediately if |
| 585 | * pti_request_masterchannel() fails, |
| 586 | * before assigning the value to filp->private_data. |
| 587 | * Slightly easier to debug if this driver needs debugging. |
| 588 | */ |
J Freyensee | 8168e9c | 2011-06-17 15:09:53 -0700 | [diff] [blame] | 589 | mc = pti_request_masterchannel(0, NULL); |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 590 | if (mc == NULL) |
| 591 | return -ENOMEM; |
| 592 | filp->private_data = mc; |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * pti_char_release()- Close a char channel to the PTI device. Part |
| 598 | * of the misc device implementation. |
| 599 | * |
| 600 | * @inode: Not used in this implementaiton. |
| 601 | * @filp: Contains private_data that contains the master, channel |
| 602 | * ID to be released by the PTI device. |
| 603 | * |
| 604 | * Returns: |
| 605 | * always 0 |
| 606 | */ |
| 607 | static int pti_char_release(struct inode *inode, struct file *filp) |
| 608 | { |
| 609 | pti_release_masterchannel(filp->private_data); |
J Freyensee | 29021bcc | 2011-05-25 14:38:18 -0700 | [diff] [blame] | 610 | filp->private_data = NULL; |
J Freyensee | 0b61d2a | 2011-05-06 16:56:49 -0700 | [diff] [blame] | 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | /** |
| 615 | * pti_char_write()- Write trace debugging data through the char |
| 616 | * interface to the PTI HW. Part of the misc device implementation. |
| 617 | * |
| 618 | * @filp: Contains private data which is used to obtain |
| 619 | * master, channel write ID. |
| 620 | * @data: trace data to be written. |
| 621 | * @len: # of byte to write. |
| 622 | * @ppose: Not used in this function implementation. |
| 623 | * |
| 624 | * Returns: |
| 625 | * int, # of bytes written |
| 626 | * otherwise, error value |
| 627 | * |
| 628 | * Notes: From side discussions with Alan Cox and experimenting |
| 629 | * with PTI debug HW like Nokia's Fido box and Lauterbach |
| 630 | * devices, 8192 byte write buffer used by USER_COPY_SIZE was |
| 631 | * deemed an appropriate size for this type of usage with |
| 632 | * debugging HW. |
| 633 | */ |
| 634 | static ssize_t pti_char_write(struct file *filp, const char __user *data, |
| 635 | size_t len, loff_t *ppose) |
| 636 | { |
| 637 | struct pti_masterchannel *mc; |
| 638 | void *kbuf; |
| 639 | const char __user *tmp; |
| 640 | size_t size = USER_COPY_SIZE; |
| 641 | size_t n = 0; |
| 642 | |
| 643 | tmp = data; |
| 644 | mc = filp->private_data; |
| 645 | |
| 646 | kbuf = kmalloc(size, GFP_KERNEL); |
| 647 | if (kbuf == NULL) { |
| 648 | pr_err("%s(%d): buf allocation failed\n", |
| 649 | __func__, __LINE__); |
| 650 | return -ENOMEM; |
| 651 | } |
| 652 | |
| 653 | do { |
| 654 | if (len - n > USER_COPY_SIZE) |
| 655 | size = USER_COPY_SIZE; |
| 656 | else |
| 657 | size = len - n; |
| 658 | |
| 659 | if (copy_from_user(kbuf, tmp, size)) { |
| 660 | kfree(kbuf); |
| 661 | return n ? n : -EFAULT; |
| 662 | } |
| 663 | |
| 664 | pti_write_to_aperture(mc, kbuf, size); |
| 665 | n += size; |
| 666 | tmp += size; |
| 667 | |
| 668 | } while (len > n); |
| 669 | |
| 670 | kfree(kbuf); |
| 671 | return len; |
| 672 | } |
| 673 | |
| 674 | static const struct tty_operations pti_tty_driver_ops = { |
| 675 | .open = pti_tty_driver_open, |
| 676 | .close = pti_tty_driver_close, |
| 677 | .write = pti_tty_driver_write, |
| 678 | .write_room = pti_tty_write_room, |
| 679 | .install = pti_tty_install, |
| 680 | .cleanup = pti_tty_cleanup |
| 681 | }; |
| 682 | |
| 683 | static const struct file_operations pti_char_driver_ops = { |
| 684 | .owner = THIS_MODULE, |
| 685 | .write = pti_char_write, |
| 686 | .open = pti_char_open, |
| 687 | .release = pti_char_release, |
| 688 | }; |
| 689 | |
| 690 | static struct miscdevice pti_char_driver = { |
| 691 | .minor = MISC_DYNAMIC_MINOR, |
| 692 | .name = CHARNAME, |
| 693 | .fops = &pti_char_driver_ops |
| 694 | }; |
| 695 | |
| 696 | /** |
| 697 | * pti_console_write()- Write to the console that has been acquired. |
| 698 | * |
| 699 | * @c: Not used in this implementaiton. |
| 700 | * @buf: Data to be written. |
| 701 | * @len: Length of buf. |
| 702 | */ |
| 703 | static void pti_console_write(struct console *c, const char *buf, unsigned len) |
| 704 | { |
| 705 | static struct pti_masterchannel mc = {.master = CONSOLE_ID, |
| 706 | .channel = 0}; |
| 707 | |
| 708 | mc.channel = pti_console_channel; |
| 709 | pti_console_channel = (pti_console_channel + 1) & 0x7f; |
| 710 | |
| 711 | pti_write_full_frame_to_aperture(&mc, buf, len); |
| 712 | } |
| 713 | |
| 714 | /** |
| 715 | * pti_console_device()- Return the driver tty structure and set the |
| 716 | * associated index implementation. |
| 717 | * |
| 718 | * @c: Console device of the driver. |
| 719 | * @index: index associated with c. |
| 720 | * |
| 721 | * Returns: |
| 722 | * always value of pti_tty_driver structure when this function |
| 723 | * is called. |
| 724 | */ |
| 725 | static struct tty_driver *pti_console_device(struct console *c, int *index) |
| 726 | { |
| 727 | *index = c->index; |
| 728 | return pti_tty_driver; |
| 729 | } |
| 730 | |
| 731 | /** |
| 732 | * pti_console_setup()- Initialize console variables used by the driver. |
| 733 | * |
| 734 | * @c: Not used. |
| 735 | * @opts: Not used. |
| 736 | * |
| 737 | * Returns: |
| 738 | * always 0. |
| 739 | */ |
| 740 | static int pti_console_setup(struct console *c, char *opts) |
| 741 | { |
| 742 | pti_console_channel = 0; |
| 743 | pti_control_channel = 0; |
| 744 | return 0; |
| 745 | } |
| 746 | |
| 747 | /* |
| 748 | * pti_console struct, used to capture OS printk()'s and shift |
| 749 | * out to the PTI device for debugging. This cannot be |
| 750 | * enabled upon boot because of the possibility of eating |
| 751 | * any serial console printk's (race condition discovered). |
| 752 | * The console should be enabled upon when the tty port is |
| 753 | * used for the first time. Since the primary purpose for |
| 754 | * the tty port is to hook up syslog to it, the tty port |
| 755 | * will be open for a really long time. |
| 756 | */ |
| 757 | static struct console pti_console = { |
| 758 | .name = TTYNAME, |
| 759 | .write = pti_console_write, |
| 760 | .device = pti_console_device, |
| 761 | .setup = pti_console_setup, |
| 762 | .flags = CON_PRINTBUFFER, |
| 763 | .index = 0, |
| 764 | }; |
| 765 | |
| 766 | /** |
| 767 | * pti_port_activate()- Used to start/initialize any items upon |
| 768 | * first opening of tty_port(). |
| 769 | * |
| 770 | * @port- The tty port number of the PTI device. |
| 771 | * @tty- The tty struct associated with this device. |
| 772 | * |
| 773 | * Returns: |
| 774 | * always returns 0 |
| 775 | * |
| 776 | * Notes: The primary purpose of the PTI tty port 0 is to hook |
| 777 | * the syslog daemon to it; thus this port will be open for a |
| 778 | * very long time. |
| 779 | */ |
| 780 | static int pti_port_activate(struct tty_port *port, struct tty_struct *tty) |
| 781 | { |
| 782 | if (port->tty->index == PTITTY_MINOR_START) |
| 783 | console_start(&pti_console); |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | /** |
| 788 | * pti_port_shutdown()- Used to stop/shutdown any items upon the |
| 789 | * last tty port close. |
| 790 | * |
| 791 | * @port- The tty port number of the PTI device. |
| 792 | * |
| 793 | * Notes: The primary purpose of the PTI tty port 0 is to hook |
| 794 | * the syslog daemon to it; thus this port will be open for a |
| 795 | * very long time. |
| 796 | */ |
| 797 | static void pti_port_shutdown(struct tty_port *port) |
| 798 | { |
| 799 | if (port->tty->index == PTITTY_MINOR_START) |
| 800 | console_stop(&pti_console); |
| 801 | } |
| 802 | |
| 803 | static const struct tty_port_operations tty_port_ops = { |
| 804 | .activate = pti_port_activate, |
| 805 | .shutdown = pti_port_shutdown, |
| 806 | }; |
| 807 | |
| 808 | /* |
| 809 | * Note the _probe() call sets everything up and ties the char and tty |
| 810 | * to successfully detecting the PTI device on the pci bus. |
| 811 | */ |
| 812 | |
| 813 | /** |
| 814 | * pti_pci_probe()- Used to detect pti on the pci bus and set |
| 815 | * things up in the driver. |
| 816 | * |
| 817 | * @pdev- pci_dev struct values for pti. |
| 818 | * @ent- pci_device_id struct for pti driver. |
| 819 | * |
| 820 | * Returns: |
| 821 | * 0 for success |
| 822 | * otherwise, error |
| 823 | */ |
| 824 | static int __devinit pti_pci_probe(struct pci_dev *pdev, |
| 825 | const struct pci_device_id *ent) |
| 826 | { |
| 827 | int retval = -EINVAL; |
| 828 | int pci_bar = 1; |
| 829 | |
| 830 | dev_dbg(&pdev->dev, "%s %s(%d): PTI PCI ID %04x:%04x\n", __FILE__, |
| 831 | __func__, __LINE__, pdev->vendor, pdev->device); |
| 832 | |
| 833 | retval = misc_register(&pti_char_driver); |
| 834 | if (retval) { |
| 835 | pr_err("%s(%d): CHAR registration failed of pti driver\n", |
| 836 | __func__, __LINE__); |
| 837 | pr_err("%s(%d): Error value returned: %d\n", |
| 838 | __func__, __LINE__, retval); |
| 839 | return retval; |
| 840 | } |
| 841 | |
| 842 | retval = pci_enable_device(pdev); |
| 843 | if (retval != 0) { |
| 844 | dev_err(&pdev->dev, |
| 845 | "%s: pci_enable_device() returned error %d\n", |
| 846 | __func__, retval); |
| 847 | return retval; |
| 848 | } |
| 849 | |
| 850 | drv_data = kzalloc(sizeof(*drv_data), GFP_KERNEL); |
| 851 | |
| 852 | if (drv_data == NULL) { |
| 853 | retval = -ENOMEM; |
| 854 | dev_err(&pdev->dev, |
| 855 | "%s(%d): kmalloc() returned NULL memory.\n", |
| 856 | __func__, __LINE__); |
| 857 | return retval; |
| 858 | } |
| 859 | drv_data->pti_addr = pci_resource_start(pdev, pci_bar); |
| 860 | |
| 861 | retval = pci_request_region(pdev, pci_bar, dev_name(&pdev->dev)); |
| 862 | if (retval != 0) { |
| 863 | dev_err(&pdev->dev, |
| 864 | "%s(%d): pci_request_region() returned error %d\n", |
| 865 | __func__, __LINE__, retval); |
| 866 | kfree(drv_data); |
| 867 | return retval; |
| 868 | } |
| 869 | drv_data->aperture_base = drv_data->pti_addr+APERTURE_14; |
| 870 | drv_data->pti_ioaddr = |
| 871 | ioremap_nocache((u32)drv_data->aperture_base, |
| 872 | APERTURE_LEN); |
| 873 | if (!drv_data->pti_ioaddr) { |
| 874 | pci_release_region(pdev, pci_bar); |
| 875 | retval = -ENOMEM; |
| 876 | kfree(drv_data); |
| 877 | return retval; |
| 878 | } |
| 879 | |
| 880 | pci_set_drvdata(pdev, drv_data); |
| 881 | |
| 882 | tty_port_init(&drv_data->port); |
| 883 | drv_data->port.ops = &tty_port_ops; |
| 884 | |
| 885 | tty_register_device(pti_tty_driver, 0, &pdev->dev); |
| 886 | tty_register_device(pti_tty_driver, 1, &pdev->dev); |
| 887 | |
| 888 | register_console(&pti_console); |
| 889 | |
| 890 | return retval; |
| 891 | } |
| 892 | |
| 893 | static struct pci_driver pti_pci_driver = { |
| 894 | .name = PCINAME, |
| 895 | .id_table = pci_ids, |
| 896 | .probe = pti_pci_probe, |
| 897 | .remove = pti_pci_remove, |
| 898 | }; |
| 899 | |
| 900 | /** |
| 901 | * |
| 902 | * pti_init()- Overall entry/init call to the pti driver. |
| 903 | * It starts the registration process with the kernel. |
| 904 | * |
| 905 | * Returns: |
| 906 | * int __init, 0 for success |
| 907 | * otherwise value is an error |
| 908 | * |
| 909 | */ |
| 910 | static int __init pti_init(void) |
| 911 | { |
| 912 | int retval = -EINVAL; |
| 913 | |
| 914 | /* First register module as tty device */ |
| 915 | |
| 916 | pti_tty_driver = alloc_tty_driver(1); |
| 917 | if (pti_tty_driver == NULL) { |
| 918 | pr_err("%s(%d): Memory allocation failed for ptiTTY driver\n", |
| 919 | __func__, __LINE__); |
| 920 | return -ENOMEM; |
| 921 | } |
| 922 | |
| 923 | pti_tty_driver->owner = THIS_MODULE; |
| 924 | pti_tty_driver->magic = TTY_DRIVER_MAGIC; |
| 925 | pti_tty_driver->driver_name = DRIVERNAME; |
| 926 | pti_tty_driver->name = TTYNAME; |
| 927 | pti_tty_driver->major = 0; |
| 928 | pti_tty_driver->minor_start = PTITTY_MINOR_START; |
| 929 | pti_tty_driver->minor_num = PTITTY_MINOR_NUM; |
| 930 | pti_tty_driver->num = PTITTY_MINOR_NUM; |
| 931 | pti_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM; |
| 932 | pti_tty_driver->subtype = SYSTEM_TYPE_SYSCONS; |
| 933 | pti_tty_driver->flags = TTY_DRIVER_REAL_RAW | |
| 934 | TTY_DRIVER_DYNAMIC_DEV; |
| 935 | pti_tty_driver->init_termios = tty_std_termios; |
| 936 | |
| 937 | tty_set_operations(pti_tty_driver, &pti_tty_driver_ops); |
| 938 | |
| 939 | retval = tty_register_driver(pti_tty_driver); |
| 940 | if (retval) { |
| 941 | pr_err("%s(%d): TTY registration failed of pti driver\n", |
| 942 | __func__, __LINE__); |
| 943 | pr_err("%s(%d): Error value returned: %d\n", |
| 944 | __func__, __LINE__, retval); |
| 945 | |
| 946 | pti_tty_driver = NULL; |
| 947 | return retval; |
| 948 | } |
| 949 | |
| 950 | retval = pci_register_driver(&pti_pci_driver); |
| 951 | |
| 952 | if (retval) { |
| 953 | pr_err("%s(%d): PCI registration failed of pti driver\n", |
| 954 | __func__, __LINE__); |
| 955 | pr_err("%s(%d): Error value returned: %d\n", |
| 956 | __func__, __LINE__, retval); |
| 957 | |
| 958 | tty_unregister_driver(pti_tty_driver); |
| 959 | pr_err("%s(%d): Unregistering TTY part of pti driver\n", |
| 960 | __func__, __LINE__); |
| 961 | pti_tty_driver = NULL; |
| 962 | return retval; |
| 963 | } |
| 964 | |
| 965 | return retval; |
| 966 | } |
| 967 | |
| 968 | /** |
| 969 | * pti_exit()- Unregisters this module as a tty and pci driver. |
| 970 | */ |
| 971 | static void __exit pti_exit(void) |
| 972 | { |
| 973 | int retval; |
| 974 | |
| 975 | tty_unregister_device(pti_tty_driver, 0); |
| 976 | tty_unregister_device(pti_tty_driver, 1); |
| 977 | |
| 978 | retval = tty_unregister_driver(pti_tty_driver); |
| 979 | if (retval) { |
| 980 | pr_err("%s(%d): TTY unregistration failed of pti driver\n", |
| 981 | __func__, __LINE__); |
| 982 | pr_err("%s(%d): Error value returned: %d\n", |
| 983 | __func__, __LINE__, retval); |
| 984 | } |
| 985 | |
| 986 | pci_unregister_driver(&pti_pci_driver); |
| 987 | |
| 988 | retval = misc_deregister(&pti_char_driver); |
| 989 | if (retval) { |
| 990 | pr_err("%s(%d): CHAR unregistration failed of pti driver\n", |
| 991 | __func__, __LINE__); |
| 992 | pr_err("%s(%d): Error value returned: %d\n", |
| 993 | __func__, __LINE__, retval); |
| 994 | } |
| 995 | |
| 996 | unregister_console(&pti_console); |
| 997 | return; |
| 998 | } |
| 999 | |
| 1000 | module_init(pti_init); |
| 1001 | module_exit(pti_exit); |
| 1002 | |
| 1003 | MODULE_LICENSE("GPL"); |
| 1004 | MODULE_AUTHOR("Ken Mills, Jay Freyensee"); |
| 1005 | MODULE_DESCRIPTION("PTI Driver"); |
| 1006 | |