/*1* pti.c - PTI driver for cJTAG data extration2*3* Copyright (C) Intel 20104*5* This program is free software; you can redistribute it and/or modify6* it under the terms of the GNU General Public License version 2 as7* published by the Free Software Foundation.8*9* This program is distributed in the hope that it will be useful,10* but WITHOUT ANY WARRANTY; without even the implied warranty of11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12* GNU General Public License for more details.13*14* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~15*16* The PTI (Parallel Trace Interface) driver directs trace data routed from17* various parts in the system out through the Intel Penwell PTI port and18* out of the mobile device for analysis with a debugging tool19* (Lauterbach, Fido). This is part of a solution for the MIPI P1149.7,20* compact JTAG, standard.21*/2223#include <linux/init.h>24#include <linux/sched.h>25#include <linux/interrupt.h>26#include <linux/console.h>27#include <linux/kernel.h>28#include <linux/module.h>29#include <linux/tty.h>30#include <linux/tty_driver.h>31#include <linux/pci.h>32#include <linux/mutex.h>33#include <linux/miscdevice.h>34#include <linux/pti.h>3536#define DRIVERNAME "pti"37#define PCINAME "pciPTI"38#define TTYNAME "ttyPTI"39#define CHARNAME "pti"40#define PTITTY_MINOR_START 041#define PTITTY_MINOR_NUM 242#define MAX_APP_IDS 16 /* 128 channel ids / u8 bit size */43#define MAX_OS_IDS 16 /* 128 channel ids / u8 bit size */44#define MAX_MODEM_IDS 16 /* 128 channel ids / u8 bit size */45#define MODEM_BASE_ID 71 /* modem master ID address */46#define CONTROL_ID 72 /* control master ID address */47#define CONSOLE_ID 73 /* console master ID address */48#define OS_BASE_ID 74 /* base OS master ID address */49#define APP_BASE_ID 80 /* base App master ID address */50#define CONTROL_FRAME_LEN 32 /* PTI control frame maximum size */51#define USER_COPY_SIZE 8192 /* 8Kb buffer for user space copy */52#define APERTURE_14 0x3800000 /* offset to first OS write addr */53#define APERTURE_LEN 0x400000 /* address length */5455struct pti_tty {56struct pti_masterchannel *mc;57};5859struct pti_dev {60struct tty_port port;61unsigned long pti_addr;62unsigned long aperture_base;63void __iomem *pti_ioaddr;64u8 ia_app[MAX_APP_IDS];65u8 ia_os[MAX_OS_IDS];66u8 ia_modem[MAX_MODEM_IDS];67};6869/*70* This protects access to ia_app, ia_os, and ia_modem,71* which keeps track of channels allocated in72* an aperture write id.73*/74static DEFINE_MUTEX(alloclock);7576static struct pci_device_id pci_ids[] __devinitconst = {77{PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x82B)},78{0}79};8081static struct tty_driver *pti_tty_driver;82static struct pti_dev *drv_data;8384static unsigned int pti_console_channel;85static unsigned int pti_control_channel;8687/**88* pti_write_to_aperture()- The private write function to PTI HW.89*90* @mc: The 'aperture'. It's part of a write address that holds91* a master and channel ID.92* @buf: Data being written to the HW that will ultimately be seen93* in a debugging tool (Fido, Lauterbach).94* @len: Size of buffer.95*96* Since each aperture is specified by a unique97* master/channel ID, no two processes will be writing98* to the same aperture at the same time so no lock is required. The99* PTI-Output agent will send these out in the order that they arrived, and100* thus, it will intermix these messages. The debug tool can then later101* regroup the appropriate message segments together reconstituting each102* message.103*/104static void pti_write_to_aperture(struct pti_masterchannel *mc,105u8 *buf,106int len)107{108int dwordcnt;109int final;110int i;111u32 ptiword;112u32 __iomem *aperture;113u8 *p = buf;114115/*116* calculate the aperture offset from the base using the master and117* channel id's.118*/119aperture = drv_data->pti_ioaddr + (mc->master << 15)120+ (mc->channel << 8);121122dwordcnt = len >> 2;123final = len - (dwordcnt << 2); /* final = trailing bytes */124if (final == 0 && dwordcnt != 0) { /* always need a final dword */125final += 4;126dwordcnt--;127}128129for (i = 0; i < dwordcnt; i++) {130ptiword = be32_to_cpu(*(u32 *)p);131p += 4;132iowrite32(ptiword, aperture);133}134135aperture += PTI_LASTDWORD_DTS; /* adding DTS signals that is EOM */136137ptiword = 0;138for (i = 0; i < final; i++)139ptiword |= *p++ << (24-(8*i));140141iowrite32(ptiword, aperture);142return;143}144145/**146* pti_control_frame_built_and_sent()- control frame build and send function.147*148* @mc: The master / channel structure on which the function149* built a control frame.150*151* To be able to post process the PTI contents on host side, a control frame152* is added before sending any PTI content. So the host side knows on153* each PTI frame the name of the thread using a dedicated master / channel.154* The thread name is retrieved from the 'current' global variable.155* This function builds this frame and sends it to a master ID CONTROL_ID.156* The overhead is only 32 bytes since the driver only writes to HW157* in 32 byte chunks.158*/159160static void pti_control_frame_built_and_sent(struct pti_masterchannel *mc)161{162struct pti_masterchannel mccontrol = {.master = CONTROL_ID,163.channel = 0};164const char *control_format = "%3d %3d %s";165u8 control_frame[CONTROL_FRAME_LEN];166167/*168* Since we access the comm member in current's task_struct,169* we only need to be as large as what 'comm' in that170* structure is.171*/172char comm[TASK_COMM_LEN];173174if (!in_interrupt())175get_task_comm(comm, current);176else177strncpy(comm, "Interrupt", TASK_COMM_LEN);178179/* Absolutely ensure our buffer is zero terminated. */180comm[TASK_COMM_LEN-1] = 0;181182mccontrol.channel = pti_control_channel;183pti_control_channel = (pti_control_channel + 1) & 0x7f;184185snprintf(control_frame, CONTROL_FRAME_LEN, control_format, mc->master,186mc->channel, comm);187pti_write_to_aperture(&mccontrol, control_frame, strlen(control_frame));188}189190/**191* pti_write_full_frame_to_aperture()- high level function to192* write to PTI.193*194* @mc: The 'aperture'. It's part of a write address that holds195* a master and channel ID.196* @buf: Data being written to the HW that will ultimately be seen197* in a debugging tool (Fido, Lauterbach).198* @len: Size of buffer.199*200* All threads sending data (either console, user space application, ...)201* are calling the high level function to write to PTI meaning that it is202* possible to add a control frame before sending the content.203*/204static void pti_write_full_frame_to_aperture(struct pti_masterchannel *mc,205const unsigned char *buf,206int len)207{208pti_control_frame_built_and_sent(mc);209pti_write_to_aperture(mc, (u8 *)buf, len);210}211212/**213* get_id()- Allocate a master and channel ID.214*215* @id_array: an array of bits representing what channel216* id's are allocated for writing.217* @max_ids: The max amount of available write IDs to use.218* @base_id: The starting SW channel ID, based on the Intel219* PTI arch.220*221* Returns:222* pti_masterchannel struct with master, channel ID address223* 0 for error224*225* Each bit in the arrays ia_app and ia_os correspond to a master and226* channel id. The bit is one if the id is taken and 0 if free. For227* every master there are 128 channel id's.228*/229static struct pti_masterchannel *get_id(u8 *id_array, int max_ids, int base_id)230{231struct pti_masterchannel *mc;232int i, j, mask;233234mc = kmalloc(sizeof(struct pti_masterchannel), GFP_KERNEL);235if (mc == NULL)236return NULL;237238/* look for a byte with a free bit */239for (i = 0; i < max_ids; i++)240if (id_array[i] != 0xff)241break;242if (i == max_ids) {243kfree(mc);244return NULL;245}246/* find the bit in the 128 possible channel opportunities */247mask = 0x80;248for (j = 0; j < 8; j++) {249if ((id_array[i] & mask) == 0)250break;251mask >>= 1;252}253254/* grab it */255id_array[i] |= mask;256mc->master = base_id;257mc->channel = ((i & 0xf)<<3) + j;258/* write new master Id / channel Id allocation to channel control */259pti_control_frame_built_and_sent(mc);260return mc;261}262263/*264* The following three functions:265* pti_request_mastercahannel(), mipi_release_masterchannel()266* and pti_writedata() are an API for other kernel drivers to267* access PTI.268*/269270/**271* pti_request_masterchannel()- Kernel API function used to allocate272* a master, channel ID address273* to write to PTI HW.274*275* @type: 0- request Application master, channel aperture ID write address.276* 1- request OS master, channel aperture ID write277* address.278* 2- request Modem master, channel aperture ID279* write address.280* Other values, error.281*282* Returns:283* pti_masterchannel struct284* 0 for error285*/286struct pti_masterchannel *pti_request_masterchannel(u8 type)287{288struct pti_masterchannel *mc;289290mutex_lock(&alloclock);291292switch (type) {293294case 0:295mc = get_id(drv_data->ia_app, MAX_APP_IDS, APP_BASE_ID);296break;297298case 1:299mc = get_id(drv_data->ia_os, MAX_OS_IDS, OS_BASE_ID);300break;301302case 2:303mc = get_id(drv_data->ia_modem, MAX_MODEM_IDS, MODEM_BASE_ID);304break;305default:306mc = NULL;307}308309mutex_unlock(&alloclock);310return mc;311}312EXPORT_SYMBOL_GPL(pti_request_masterchannel);313314/**315* pti_release_masterchannel()- Kernel API function used to release316* a master, channel ID address317* used to write to PTI HW.318*319* @mc: master, channel apeture ID address to be released. This320* will de-allocate the structure via kfree().321*/322void pti_release_masterchannel(struct pti_masterchannel *mc)323{324u8 master, channel, i;325326mutex_lock(&alloclock);327328if (mc) {329master = mc->master;330channel = mc->channel;331332if (master == APP_BASE_ID) {333i = channel >> 3;334drv_data->ia_app[i] &= ~(0x80>>(channel & 0x7));335} else if (master == OS_BASE_ID) {336i = channel >> 3;337drv_data->ia_os[i] &= ~(0x80>>(channel & 0x7));338} else {339i = channel >> 3;340drv_data->ia_modem[i] &= ~(0x80>>(channel & 0x7));341}342343kfree(mc);344}345346mutex_unlock(&alloclock);347}348EXPORT_SYMBOL_GPL(pti_release_masterchannel);349350/**351* pti_writedata()- Kernel API function used to write trace352* debugging data to PTI HW.353*354* @mc: Master, channel aperture ID address to write to.355* Null value will return with no write occurring.356* @buf: Trace debuging data to write to the PTI HW.357* Null value will return with no write occurring.358* @count: Size of buf. Value of 0 or a negative number will359* return with no write occuring.360*/361void pti_writedata(struct pti_masterchannel *mc, u8 *buf, int count)362{363/*364* since this function is exported, this is treated like an365* API function, thus, all parameters should366* be checked for validity.367*/368if ((mc != NULL) && (buf != NULL) && (count > 0))369pti_write_to_aperture(mc, buf, count);370return;371}372EXPORT_SYMBOL_GPL(pti_writedata);373374/**375* pti_pci_remove()- Driver exit method to remove PTI from376* PCI bus.377* @pdev: variable containing pci info of PTI.378*/379static void __devexit pti_pci_remove(struct pci_dev *pdev)380{381struct pti_dev *drv_data;382383drv_data = pci_get_drvdata(pdev);384if (drv_data != NULL) {385pci_iounmap(pdev, drv_data->pti_ioaddr);386pci_set_drvdata(pdev, NULL);387kfree(drv_data);388pci_release_region(pdev, 1);389pci_disable_device(pdev);390}391}392393/*394* for the tty_driver_*() basic function descriptions, see tty_driver.h.395* Specific header comments made for PTI-related specifics.396*/397398/**399* pti_tty_driver_open()- Open an Application master, channel aperture400* ID to the PTI device via tty device.401*402* @tty: tty interface.403* @filp: filp interface pased to tty_port_open() call.404*405* Returns:406* int, 0 for success407* otherwise, fail value408*409* The main purpose of using the tty device interface is for410* each tty port to have a unique PTI write aperture. In an411* example use case, ttyPTI0 gets syslogd and an APP aperture412* ID and ttyPTI1 is where the n_tracesink ldisc hooks to route413* modem messages into PTI. Modem trace data does not have to414* go to ttyPTI1, but ttyPTI0 and ttyPTI1 do need to be distinct415* master IDs. These messages go through the PTI HW and out of416* the handheld platform and to the Fido/Lauterbach device.417*/418static int pti_tty_driver_open(struct tty_struct *tty, struct file *filp)419{420/*421* we actually want to allocate a new channel per open, per422* system arch. HW gives more than plenty channels for a single423* system task to have its own channel to write trace data. This424* also removes a locking requirement for the actual write425* procedure.426*/427return tty_port_open(&drv_data->port, tty, filp);428}429430/**431* pti_tty_driver_close()- close tty device and release Application432* master, channel aperture ID to the PTI device via tty device.433*434* @tty: tty interface.435* @filp: filp interface pased to tty_port_close() call.436*437* The main purpose of using the tty device interface is to route438* syslog daemon messages to the PTI HW and out of the handheld platform439* and to the Fido/Lauterbach device.440*/441static void pti_tty_driver_close(struct tty_struct *tty, struct file *filp)442{443tty_port_close(&drv_data->port, tty, filp);444}445446/**447* pti_tty_intstall()- Used to set up specific master-channels448* to tty ports for organizational purposes when449* tracing viewed from debuging tools.450*451* @driver: tty driver information.452* @tty: tty struct containing pti information.453*454* Returns:455* 0 for success456* otherwise, error457*/458static int pti_tty_install(struct tty_driver *driver, struct tty_struct *tty)459{460int idx = tty->index;461struct pti_tty *pti_tty_data;462int ret = tty_init_termios(tty);463464if (ret == 0) {465tty_driver_kref_get(driver);466tty->count++;467driver->ttys[idx] = tty;468469pti_tty_data = kmalloc(sizeof(struct pti_tty), GFP_KERNEL);470if (pti_tty_data == NULL)471return -ENOMEM;472473if (idx == PTITTY_MINOR_START)474pti_tty_data->mc = pti_request_masterchannel(0);475else476pti_tty_data->mc = pti_request_masterchannel(2);477478if (pti_tty_data->mc == NULL) {479kfree(pti_tty_data);480return -ENXIO;481}482tty->driver_data = pti_tty_data;483}484485return ret;486}487488/**489* pti_tty_cleanup()- Used to de-allocate master-channel resources490* tied to tty's of this driver.491*492* @tty: tty struct containing pti information.493*/494static void pti_tty_cleanup(struct tty_struct *tty)495{496struct pti_tty *pti_tty_data = tty->driver_data;497if (pti_tty_data == NULL)498return;499pti_release_masterchannel(pti_tty_data->mc);500kfree(pti_tty_data);501tty->driver_data = NULL;502}503504/**505* pti_tty_driver_write()- Write trace debugging data through the char506* interface to the PTI HW. Part of the misc device implementation.507*508* @filp: Contains private data which is used to obtain509* master, channel write ID.510* @data: trace data to be written.511* @len: # of byte to write.512*513* Returns:514* int, # of bytes written515* otherwise, error516*/517static int pti_tty_driver_write(struct tty_struct *tty,518const unsigned char *buf, int len)519{520struct pti_tty *pti_tty_data = tty->driver_data;521if ((pti_tty_data != NULL) && (pti_tty_data->mc != NULL)) {522pti_write_to_aperture(pti_tty_data->mc, (u8 *)buf, len);523return len;524}525/*526* we can't write to the pti hardware if the private driver_data527* and the mc address is not there.528*/529else530return -EFAULT;531}532533/**534* pti_tty_write_room()- Always returns 2048.535*536* @tty: contains tty info of the pti driver.537*/538static int pti_tty_write_room(struct tty_struct *tty)539{540return 2048;541}542543/**544* pti_char_open()- Open an Application master, channel aperture545* ID to the PTI device. Part of the misc device implementation.546*547* @inode: not used.548* @filp: Output- will have a masterchannel struct set containing549* the allocated application PTI aperture write address.550*551* Returns:552* int, 0 for success553* otherwise, a fail value554*/555static int pti_char_open(struct inode *inode, struct file *filp)556{557struct pti_masterchannel *mc;558559/*560* We really do want to fail immediately if561* pti_request_masterchannel() fails,562* before assigning the value to filp->private_data.563* Slightly easier to debug if this driver needs debugging.564*/565mc = pti_request_masterchannel(0);566if (mc == NULL)567return -ENOMEM;568filp->private_data = mc;569return 0;570}571572/**573* pti_char_release()- Close a char channel to the PTI device. Part574* of the misc device implementation.575*576* @inode: Not used in this implementaiton.577* @filp: Contains private_data that contains the master, channel578* ID to be released by the PTI device.579*580* Returns:581* always 0582*/583static int pti_char_release(struct inode *inode, struct file *filp)584{585pti_release_masterchannel(filp->private_data);586filp->private_data = NULL;587return 0;588}589590/**591* pti_char_write()- Write trace debugging data through the char592* interface to the PTI HW. Part of the misc device implementation.593*594* @filp: Contains private data which is used to obtain595* master, channel write ID.596* @data: trace data to be written.597* @len: # of byte to write.598* @ppose: Not used in this function implementation.599*600* Returns:601* int, # of bytes written602* otherwise, error value603*604* Notes: From side discussions with Alan Cox and experimenting605* with PTI debug HW like Nokia's Fido box and Lauterbach606* devices, 8192 byte write buffer used by USER_COPY_SIZE was607* deemed an appropriate size for this type of usage with608* debugging HW.609*/610static ssize_t pti_char_write(struct file *filp, const char __user *data,611size_t len, loff_t *ppose)612{613struct pti_masterchannel *mc;614void *kbuf;615const char __user *tmp;616size_t size = USER_COPY_SIZE;617size_t n = 0;618619tmp = data;620mc = filp->private_data;621622kbuf = kmalloc(size, GFP_KERNEL);623if (kbuf == NULL) {624pr_err("%s(%d): buf allocation failed\n",625__func__, __LINE__);626return -ENOMEM;627}628629do {630if (len - n > USER_COPY_SIZE)631size = USER_COPY_SIZE;632else633size = len - n;634635if (copy_from_user(kbuf, tmp, size)) {636kfree(kbuf);637return n ? n : -EFAULT;638}639640pti_write_to_aperture(mc, kbuf, size);641n += size;642tmp += size;643644} while (len > n);645646kfree(kbuf);647return len;648}649650static const struct tty_operations pti_tty_driver_ops = {651.open = pti_tty_driver_open,652.close = pti_tty_driver_close,653.write = pti_tty_driver_write,654.write_room = pti_tty_write_room,655.install = pti_tty_install,656.cleanup = pti_tty_cleanup657};658659static const struct file_operations pti_char_driver_ops = {660.owner = THIS_MODULE,661.write = pti_char_write,662.open = pti_char_open,663.release = pti_char_release,664};665666static struct miscdevice pti_char_driver = {667.minor = MISC_DYNAMIC_MINOR,668.name = CHARNAME,669.fops = &pti_char_driver_ops670};671672/**673* pti_console_write()- Write to the console that has been acquired.674*675* @c: Not used in this implementaiton.676* @buf: Data to be written.677* @len: Length of buf.678*/679static void pti_console_write(struct console *c, const char *buf, unsigned len)680{681static struct pti_masterchannel mc = {.master = CONSOLE_ID,682.channel = 0};683684mc.channel = pti_console_channel;685pti_console_channel = (pti_console_channel + 1) & 0x7f;686687pti_write_full_frame_to_aperture(&mc, buf, len);688}689690/**691* pti_console_device()- Return the driver tty structure and set the692* associated index implementation.693*694* @c: Console device of the driver.695* @index: index associated with c.696*697* Returns:698* always value of pti_tty_driver structure when this function699* is called.700*/701static struct tty_driver *pti_console_device(struct console *c, int *index)702{703*index = c->index;704return pti_tty_driver;705}706707/**708* pti_console_setup()- Initialize console variables used by the driver.709*710* @c: Not used.711* @opts: Not used.712*713* Returns:714* always 0.715*/716static int pti_console_setup(struct console *c, char *opts)717{718pti_console_channel = 0;719pti_control_channel = 0;720return 0;721}722723/*724* pti_console struct, used to capture OS printk()'s and shift725* out to the PTI device for debugging. This cannot be726* enabled upon boot because of the possibility of eating727* any serial console printk's (race condition discovered).728* The console should be enabled upon when the tty port is729* used for the first time. Since the primary purpose for730* the tty port is to hook up syslog to it, the tty port731* will be open for a really long time.732*/733static struct console pti_console = {734.name = TTYNAME,735.write = pti_console_write,736.device = pti_console_device,737.setup = pti_console_setup,738.flags = CON_PRINTBUFFER,739.index = 0,740};741742/**743* pti_port_activate()- Used to start/initialize any items upon744* first opening of tty_port().745*746* @port- The tty port number of the PTI device.747* @tty- The tty struct associated with this device.748*749* Returns:750* always returns 0751*752* Notes: The primary purpose of the PTI tty port 0 is to hook753* the syslog daemon to it; thus this port will be open for a754* very long time.755*/756static int pti_port_activate(struct tty_port *port, struct tty_struct *tty)757{758if (port->tty->index == PTITTY_MINOR_START)759console_start(&pti_console);760return 0;761}762763/**764* pti_port_shutdown()- Used to stop/shutdown any items upon the765* last tty port close.766*767* @port- The tty port number of the PTI device.768*769* Notes: The primary purpose of the PTI tty port 0 is to hook770* the syslog daemon to it; thus this port will be open for a771* very long time.772*/773static void pti_port_shutdown(struct tty_port *port)774{775if (port->tty->index == PTITTY_MINOR_START)776console_stop(&pti_console);777}778779static const struct tty_port_operations tty_port_ops = {780.activate = pti_port_activate,781.shutdown = pti_port_shutdown,782};783784/*785* Note the _probe() call sets everything up and ties the char and tty786* to successfully detecting the PTI device on the pci bus.787*/788789/**790* pti_pci_probe()- Used to detect pti on the pci bus and set791* things up in the driver.792*793* @pdev- pci_dev struct values for pti.794* @ent- pci_device_id struct for pti driver.795*796* Returns:797* 0 for success798* otherwise, error799*/800static int __devinit pti_pci_probe(struct pci_dev *pdev,801const struct pci_device_id *ent)802{803int retval = -EINVAL;804int pci_bar = 1;805806dev_dbg(&pdev->dev, "%s %s(%d): PTI PCI ID %04x:%04x\n", __FILE__,807__func__, __LINE__, pdev->vendor, pdev->device);808809retval = misc_register(&pti_char_driver);810if (retval) {811pr_err("%s(%d): CHAR registration failed of pti driver\n",812__func__, __LINE__);813pr_err("%s(%d): Error value returned: %d\n",814__func__, __LINE__, retval);815return retval;816}817818retval = pci_enable_device(pdev);819if (retval != 0) {820dev_err(&pdev->dev,821"%s: pci_enable_device() returned error %d\n",822__func__, retval);823return retval;824}825826drv_data = kzalloc(sizeof(*drv_data), GFP_KERNEL);827828if (drv_data == NULL) {829retval = -ENOMEM;830dev_err(&pdev->dev,831"%s(%d): kmalloc() returned NULL memory.\n",832__func__, __LINE__);833return retval;834}835drv_data->pti_addr = pci_resource_start(pdev, pci_bar);836837retval = pci_request_region(pdev, pci_bar, dev_name(&pdev->dev));838if (retval != 0) {839dev_err(&pdev->dev,840"%s(%d): pci_request_region() returned error %d\n",841__func__, __LINE__, retval);842kfree(drv_data);843return retval;844}845drv_data->aperture_base = drv_data->pti_addr+APERTURE_14;846drv_data->pti_ioaddr =847ioremap_nocache((u32)drv_data->aperture_base,848APERTURE_LEN);849if (!drv_data->pti_ioaddr) {850pci_release_region(pdev, pci_bar);851retval = -ENOMEM;852kfree(drv_data);853return retval;854}855856pci_set_drvdata(pdev, drv_data);857858tty_port_init(&drv_data->port);859drv_data->port.ops = &tty_port_ops;860861tty_register_device(pti_tty_driver, 0, &pdev->dev);862tty_register_device(pti_tty_driver, 1, &pdev->dev);863864register_console(&pti_console);865866return retval;867}868869static struct pci_driver pti_pci_driver = {870.name = PCINAME,871.id_table = pci_ids,872.probe = pti_pci_probe,873.remove = pti_pci_remove,874};875876/**877*878* pti_init()- Overall entry/init call to the pti driver.879* It starts the registration process with the kernel.880*881* Returns:882* int __init, 0 for success883* otherwise value is an error884*885*/886static int __init pti_init(void)887{888int retval = -EINVAL;889890/* First register module as tty device */891892pti_tty_driver = alloc_tty_driver(1);893if (pti_tty_driver == NULL) {894pr_err("%s(%d): Memory allocation failed for ptiTTY driver\n",895__func__, __LINE__);896return -ENOMEM;897}898899pti_tty_driver->owner = THIS_MODULE;900pti_tty_driver->magic = TTY_DRIVER_MAGIC;901pti_tty_driver->driver_name = DRIVERNAME;902pti_tty_driver->name = TTYNAME;903pti_tty_driver->major = 0;904pti_tty_driver->minor_start = PTITTY_MINOR_START;905pti_tty_driver->minor_num = PTITTY_MINOR_NUM;906pti_tty_driver->num = PTITTY_MINOR_NUM;907pti_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;908pti_tty_driver->subtype = SYSTEM_TYPE_SYSCONS;909pti_tty_driver->flags = TTY_DRIVER_REAL_RAW |910TTY_DRIVER_DYNAMIC_DEV;911pti_tty_driver->init_termios = tty_std_termios;912913tty_set_operations(pti_tty_driver, &pti_tty_driver_ops);914915retval = tty_register_driver(pti_tty_driver);916if (retval) {917pr_err("%s(%d): TTY registration failed of pti driver\n",918__func__, __LINE__);919pr_err("%s(%d): Error value returned: %d\n",920__func__, __LINE__, retval);921922pti_tty_driver = NULL;923return retval;924}925926retval = pci_register_driver(&pti_pci_driver);927928if (retval) {929pr_err("%s(%d): PCI registration failed of pti driver\n",930__func__, __LINE__);931pr_err("%s(%d): Error value returned: %d\n",932__func__, __LINE__, retval);933934tty_unregister_driver(pti_tty_driver);935pr_err("%s(%d): Unregistering TTY part of pti driver\n",936__func__, __LINE__);937pti_tty_driver = NULL;938return retval;939}940941return retval;942}943944/**945* pti_exit()- Unregisters this module as a tty and pci driver.946*/947static void __exit pti_exit(void)948{949int retval;950951tty_unregister_device(pti_tty_driver, 0);952tty_unregister_device(pti_tty_driver, 1);953954retval = tty_unregister_driver(pti_tty_driver);955if (retval) {956pr_err("%s(%d): TTY unregistration failed of pti driver\n",957__func__, __LINE__);958pr_err("%s(%d): Error value returned: %d\n",959__func__, __LINE__, retval);960}961962pci_unregister_driver(&pti_pci_driver);963964retval = misc_deregister(&pti_char_driver);965if (retval) {966pr_err("%s(%d): CHAR unregistration failed of pti driver\n",967__func__, __LINE__);968pr_err("%s(%d): Error value returned: %d\n",969__func__, __LINE__, retval);970}971972unregister_console(&pti_console);973return;974}975976module_init(pti_init);977module_exit(pti_exit);978979MODULE_LICENSE("GPL");980MODULE_AUTHOR("Ken Mills, Jay Freyensee");981MODULE_DESCRIPTION("PTI Driver");982983984985