aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/misc/pti.c
blob: b3c182c719c5f8ca584d8cadaeccba8421aba72c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
/*
 *  pti.c - PTI driver for cJTAG data extration
 *
 *  Copyright (C) Intel 2010
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA
 */


/*
 * The PTI (Parallel Trace Interface) driver directs trace data routed from
 * various parts in the system out through the Intel Penwell PTI port and
 * out of the mobile device for analysis with a debugging tool
 * (Lauterbach, Fido). This is part of a solution for the MIPI P1149.7,
 * compact JTAG, standard.
 */

#include <linux/init.h>
#include <linux/sched.h>
#include <linux/console.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/pci.h>
#include <linux/mutex.h>
#include <linux/miscdevice.h>
#include <linux/pti.h>

#define DRIVERNAME	"pti"
#define PCINAME		"pciPTI"
#define TTYNAME		"ttyPTI"
#define CHARNAME	"pti"
#define MAX_APP_IDS	256
#define MAX_OS_IDS	128
#define CONTROL_ID	72   /* control master ID address */
#define CONSOLE_ID	73   /* console master ID address */
#define OS_BASE_ID	74   /* base OS master ID address */
#define APP_BASE_ID	80   /* base App master ID address */
#define USER_COPY_SIZE	8196 /* 8Kb buffer to copy data from user space */

struct pti_tty {
	struct masterchannel *mc;
};

struct pti_dev {
	struct tty_port port;
	unsigned long pti_addr;
	unsigned long aperture_base;
	void __iomem *pti_ioaddr;
	unsigned long pti_iolen;
	u8 IA_App[MAX_APP_IDS];
	u8 IA_OS[MAX_OS_IDS];
};


static DEFINE_MUTEX(alloclock);

static struct pci_device_id pci_ids[] __devinitconst = {
		{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x82B) },
		{}
};

static struct tty_driver *pti_tty_driver;

static struct pti_dev *drv_data;

static unsigned int pti_console_channel;
static unsigned int pti_control_channel;

#define DTS 0x30		/* offset for last dword of a PTI message */

/**
 *  pti_write_to_aperture() - THE private write function to PTI HW.
 *  @mc: The 'aperture'. It's part of a write address that holds
 *       a master and channel ID.
 *  @buf: Data being written to the HW that will ultimately be seen
 *        in a debugging tool (Fido, Lauterbach).
 *  @len: Size of buffer.
 *
 *  Since each aperture is specified by a unique
 *  master/channel ID, no two processes will be writing
 *  to the same aperture at the same time so no lock is required. The
 *  PTI-Output agent will send these out in the order that they arrived, and
 *  thus, it will intermix these messages. The debug tool can then later
 *  regroup the appropriate message segments together reconstituting each
 *  message.
 */
static void pti_write_to_aperture(struct masterchannel *mc, u8 *buf, int len)
{
	int dwordcnt, final, i;
	union {
		u32 val;
		u8 c[4];
	} ptiword;
	u8 *p;
	u32 __iomem *aperture;

	p = buf;

	/*
	   calculate the aperture offset from the base using the master and
	   channel id's.
	*/
	aperture = drv_data->pti_ioaddr + (mc->master << 15)
		+ (mc->channel << 8);

	dwordcnt = len >> 2;
	final = len - (dwordcnt << 2);		/* final = trailing bytes */
	if (final == 0 && dwordcnt != 0) {	/* always have a final dword */
		final += 4;
		dwordcnt--;
	}

	/*
	   FIXME: This algorithm builds the dword from the input buffer.
	   This algorithm does work correctly with the PTI HW
	   and Fido debugging HW.  However, this got flagged in upstream
	   review not conforming to proper endian practices.
	   u32 ptiword = cpu_to_le32(*(u32 *)p);
	   was tried but was incorrect endianess.  Then the Fido
	   HW used to test this code broke.  The goal is to submit
	   something known to work and then fix this when it can be tested.
	*/
	for (i = 0; i < dwordcnt; i++) {
		ptiword.c[3] = *p++;
		ptiword.c[2] = *p++;
		ptiword.c[1] = *p++;
		ptiword.c[0] = *p++;
		pr_debug("%s(%d): PTI aperture: master(%d), channel(%d)\n",
			__func__, __LINE__, mc->master, mc->channel);
		pr_debug("%s(%d): PTI double word: %#x\n\n",
			__func__, __LINE__, ptiword.val);
		iowrite32(ptiword.val, aperture);
	}

	aperture += DTS;		/* adding DTS signals that is EOM */
	ptiword.val = 0;
	/*
	   FIXME: This has the same issue as stated in other FIXME.
	   u32 ptiword |= *p++ << (8 * i); was tried and had the
	   same character-swapping endianess problem.
	 */
	for (i = 0; i < final; i++)
		ptiword.c[3-i] = *p++;

	pr_debug("%s(%d): PTI aperture: master(%d), channel(%d)\n",
		__func__, __LINE__, mc->master, mc->channel);
	pr_debug("%s(%d): Final PTI double word: %#x\n\n",
		__func__, __LINE__, ptiword.val);
	iowrite32(ptiword.val, aperture);

	return;
}

/**
 *  pti_control_frame_built_and_sent() - control frame build and send function.
 *  @mc: The master / channel structure on which the function built a control
 *  frame.
 *
 *  To be able to post process the PTI contents on host side, a control frame
 *  is added before sending any PTI content. So the host side knows on
 *  each PTI frame the name of the thread using a dedicated master / channel.
 *  This function builds this frame and sends it to a master ID CONTROL_ID.
 *  The overhead is only 32 bytes since the driver only writes to HW
 *  in 32 byte chunks.
 */
static void pti_control_frame_built_and_sent(struct masterchannel *mc)
{
	struct masterchannel mccontrol = {.master = CONTROL_ID, .channel = 0};
	struct thread_info *thread;

	const char *control_format = "%3d %3d %s";

	int control_len = 32;
	char *comm;
	u8 control_frame[32];

	thread = current_thread_info();
	comm = (char *)(struct task_struct *)(thread->task)->comm;

	mccontrol.channel = pti_control_channel;
	pti_control_channel = (pti_control_channel + 1) & 0x7f;

	if (strlen(comm) < 23)
		control_len = 9 + strlen(comm);

	snprintf(control_frame, control_len, control_format, mc->master,
		mc->channel, comm);

	pti_write_to_aperture(&mccontrol, control_frame, control_len);
}


/**
 *  pti_write_full_frame_to_aperture() - high level function to write to PTI
 *  @mc: The 'aperture'. It's part of a write address that holds
 *       a master and channel ID.
 *  @buf: Data being written to the HW that will ultimately be seen
 *        in a debugging tool (Fido, Lauterbach).
 *  @len: Size of buffer.
 *
 *  All threads sending data (either console, user space application, ...)
 *  are calling the high level function to write to PTI meaning that it is
 *  possible to add a control frame before sending the content.
 */
static void pti_write_full_frame_to_aperture(struct masterchannel *mc,
						const unsigned char *buf,
						int len)
{
	pti_control_frame_built_and_sent(mc);
	pti_write_to_aperture(mc, (u8 *)buf, len);
}


/**
 * getID(): Allocate a master and channel ID.
 *
 * @IDarray:
 * @max_IDS: The max amount of available write IDs to use.
 * @baseID:  The starting SW channel ID, based on the Intel
 *           PTI arch.
 *
 * @return: masterchannel struct containing master, channel ID address,
 * or 0 for error.
 *
 * Each bit in the arrays IA_App and IA_OS correspond to a master and
 * channel id. The bit is one if the id is taken and 0 if free. For
 * every master there are 128 channel id's.
 */
static struct masterchannel *getID(u8 *IDarray, int max_IDS, int baseID)
{
	struct masterchannel *mc;
	int i, j, mask;

	mc = kmalloc(sizeof(struct masterchannel), GFP_KERNEL);
	if (mc == NULL)
		return 0;

	/* look for a byte with a free bit */
	for (i = 0; i < max_IDS; i++)
		if (IDarray[i] != 0xff)
			break;
	if (i == max_IDS)
		return 0;
	/* find the bit */
	mask = 0x80;
	for (j = 0; j < 8; j++) {
		if ((IDarray[i] & mask) == 0)
			break;
		mask >>= 1;
	}

	/* grab it */
	IDarray[i] |= mask;
	mc->master = (i>>4)+baseID;
	mc->channel = ((i & 0xf)<<3) + j;
    /* write new master Id / channel Id allocation to channel control */
    pti_control_frame_built_and_sent(mc);
	return mc;
}

/*
	The following three functions:
	mipi_request_mastercahannel(), mipi_release masterchannel()
	and mipi_write_data() are an API for other kernel drivers to
	access PTI.
*/

/**
 * mipi_request_masterchannel() - Kernel API function used to allocate
 *                                a master, channel ID address to write to
 *                                PTI HW.
 * @type: 0- request Application  master, channel aperture ID write address.
 *        1- request OS master, channel aperture ID write address.
 *        Other values, error.
 * @return: masterchannel struct or 0 for error.
 *
 */
struct masterchannel *mipi_request_masterchannel(u8 type)
{
	struct masterchannel *mc;

	mutex_lock(&alloclock);

	switch (type) {

	case 0:
		mc = getID(drv_data->IA_App, MAX_APP_IDS, APP_BASE_ID);
		break;

	case 1:
		mc = getID(drv_data->IA_OS, MAX_OS_IDS, OS_BASE_ID);
		break;

	default:
		mutex_unlock(&alloclock);
		return 0;
	}

	mutex_unlock(&alloclock);
	return mc;
}
EXPORT_SYMBOL(mipi_request_masterchannel);

/**
 * mipi_release_masterchannel() - Kernel API function used to release
 *                                a master, channel ID address
 *                                used to write to PTI HW.
 * @mc: master, channel apeture ID address to be released.
 *
 */
void mipi_release_masterchannel(struct masterchannel *mc)
{
	u8 master, channel, i;
	if (mc) {
		master = mc->master;
		channel = mc->channel;

		if (master >= 80) {
			i = ((master-80) << 4) + (channel>>3);
			drv_data->IA_App[i] &=  ~(0x80>>(channel & 0x7));
		}
		if (master >= 72) {
			i = ((master-72) << 4) + (channel>>3);
			drv_data->IA_OS[i] &= ~(0x80>>(channel & 0x7));
		}

		kfree(mc);
	}
}
EXPORT_SYMBOL(mipi_release_masterchannel);

/**
 * mipi_pti_writedata() - Kernel API function used to write trace
 *                        debugging data to PTI HW.
 *
 * @mc:    Master, channel aperture ID address to write to.
 *         Null value will return with no write occurring.
 * @buf:   Trace debuging data to write to the PTI HW.
 *         Null value will return with no write occurring.
 * @count: Size of buf. Value of 0 or a negative number will
 *         retrn with no write occuring.
 */
void mipi_pti_writedata(struct masterchannel *mc, u8 *buf, int count)
{
	/*
	   since this function is exported, this is treated like an
	   API function, thus, all parameters should
	   be checked for validity.
	*/
	if ((mc != NULL) && (buf != NULL) && (count > 0)) {
		pti_write_to_aperture(mc, buf, count);
		pr_debug("%s(%d): buf: %s, len: %d\n", __func__, __LINE__,
			buf, count);
	}
	return;
}
EXPORT_SYMBOL(mipi_pti_writedata);

static const struct tty_port_operations tty_port_ops = {
};

static void __devexit pti_pci_remove(struct pci_dev *pdev)
{
	struct pti_dev *drv_data;

	drv_data = pci_get_drvdata(pdev);
	if (drv_data != NULL) {
		pci_iounmap(pdev, drv_data->pti_ioaddr);
		pci_set_drvdata(pdev, NULL);
		kfree(drv_data);
		pci_release_region(pdev, 0);
		pci_disable_device(pdev);
	}
}

/*
   for the tty_driver_*() basic function descriptions, see tty_driver.h.
   Specific header comments made for PTI-related specifics.
*/

/**
 * pti_tty_driver_open()- Open an Application master, channel aperture
 * ID to the PTI device via tty device.
 *
 * @param tty: tty interface.
 * @param filp: filp interface pased to tty_port_open() call.
 *
 * @return int : Success = 0, otherwise fail.
 *
 * The main purpose of using the tty device interface is to route
 * syslog daemon messages to the PTI HW and out of the handheld platform
 * and to the Fido/Lauterbach device.
 */
static int pti_tty_driver_open(struct tty_struct *tty, struct file *filp)
{
	struct pti_tty *pti_tty_data;
	struct masterchannel *mc;
	int ret = 0;

	pr_debug("%s %s(%d): Called.\n", __FILE__,  __func__, __LINE__);

	/*
	   we actually want to allocate a new channel per open, per
	   system arch.  HW gives more than plenty channels for a single
	   system task to have its own channel to write trace data. This
	   also removes a locking requirement for the actual write
	   procedure.
	*/
	ret = tty_port_open(&drv_data->port, tty, filp);
	pti_tty_data = tty->driver_data;
	mc = mipi_request_masterchannel(0);
	pti_tty_data->mc = mc;

	return ret;
}

/**
 * pti_tty_driver_close()- close tty device and release Application
 * master, channel aperture ID to the PTI device via tty device.
 *
 * @param tty: tty interface.
 * @param filp: filp interface pased to tty_port_close() call.
 *
 * The main purpose of using the tty device interface is to route
 * syslog daemon messages to the PTI HW and out of the handheld platform
 * and to the Fido/Lauterbach device.
 */
static void pti_tty_driver_close(struct tty_struct *tty, struct file *filp)
{
	struct pti_tty *pti_tty_data;
	struct masterchannel *mc;

	pr_debug("%s(%d): Called.\n", __func__, __LINE__);

	pti_tty_data = tty->driver_data;
	if (pti_tty_data != NULL) {
		mc = pti_tty_data->mc;
		mipi_release_masterchannel(mc);
	}

	tty_port_close(&drv_data->port, tty, filp);

	return;
}

static int pti_tty_install(struct tty_driver *driver, struct tty_struct *tty)
{
	int idx = tty->index;
	struct pti_tty *pti_tty_data;

	int ret = tty_init_termios(tty);

	if (ret == 0) {
		tty_driver_kref_get(driver);
		tty->count++;
		driver->ttys[idx] = tty;

		pti_tty_data = kmalloc(sizeof(struct pti_tty), GFP_KERNEL);
		if (pti_tty_data == NULL)
			return -ENOMEM;

		tty->driver_data = pti_tty_data;
	}

	return ret;
}

static void pti_tty_cleanup(struct tty_struct *tty)
{
	struct pti_tty *pti_tty_data;

	pti_tty_data = tty->driver_data;
	if (pti_tty_data != NULL)
		kfree(pti_tty_data);

	tty->driver_data = NULL;
}

/**
 * pti_tty_driver_write():  Write trace debugging data through the char
 * interface to the PTI HW.  Part of the misc device implementation.
 *
 * @param filp: Contains private data which is used to obtain
 *              master, channel write ID.
 * @param data: trace data to be written.
 * @param len:  # of byte to write.
 * @param ppose: Not used in this function implementation.
 * @return int : # of bytes written, or error.  -EMSGSIZE is
 *               returned if length is beyond 8k.
 */
int pti_tty_driver_write(struct tty_struct *tty,
	const unsigned char *buf, int len)
{
	struct masterchannel *mc;
	struct pti_tty *pti_tty_data;

	pr_debug("%s(%d): buf: %s, len: %d\n", __func__, __LINE__, buf, len);

	pti_tty_data = tty->driver_data;
	mc = pti_tty_data->mc;
	pti_write_to_aperture(mc, (u8 *)buf, len);

	return len;
}

int pti_tty_write_room(struct tty_struct *tty)
{
	return 2048;
}

/**
 * pti_char_open()- Open an Application master, channel aperture
 * ID to the PTI device. Part of the misc device implementation.
 *
 * @param inode: not used.
 * @param filp: Output- will have a masterchannel struct set containing
 * the allocated application PTI aperture write address.
 *
 * @return int : Success = 0, otherwise fail.  As of right now,
 *         it is not sure what needs to really be initialized
 *         for open(), so it always returns 0.
 */
int pti_char_open(struct inode *inode, struct file *filp)
{
	struct masterchannel *mc;

	mc = mipi_request_masterchannel(0);
	if (mc == NULL)
		return -ENOMEM;
	filp->private_data = mc;
	return 0;
}

/**
 * pti_char_release()-  Close a char channel to the PTI device. Part
 * of the misc device implementation.
 *
 * @param inode: Not used in this implementaiton.
 * @param filp: Contains private_data that contains the master, channel
 * ID to be released by the PTI device.
 *
 * @return int : Success = 0
 */
int pti_char_release(struct inode *inode, struct file *filp)
{
	mipi_release_masterchannel(filp->private_data);

	return 0;
}

/**
 * pti_char_write():  Write trace debugging data through the char
 * interface to the PTI HW.  Part of the misc device implementation.
 *
 * @param filp: Contains private data which is used to obtain
 *              master, channel write ID.
 * @param data: trace data to be written.
 * @param len:  # of byte to write.
 * @param ppose: Not used in this function implementation.
 * @return int : # of bytes written, or error.  -EMSGSIZE is
 *               returned if length is beyond 8k.
 */
ssize_t pti_char_write(struct file *filp, const char *data, size_t len,
		loff_t *ppose)
{
	struct masterchannel *mc;
	void *kbuf;
	const char *tmp = data;

	size_t size = USER_COPY_SIZE, n = 0;

	mc = filp->private_data;

	kbuf = kmalloc(size, GFP_KERNEL);
	if (kbuf == NULL)  {
		pr_err("%s(%d): buf allocation failed\n",
			__func__, __LINE__);
		return 0;
	}

	do {
		if (len - n > USER_COPY_SIZE)
			size = USER_COPY_SIZE;
		else
			size = len - n;

		if (copy_from_user(kbuf, tmp, size)) {
			kfree(kbuf);
			return -EFAULT;
		}

		pr_debug("%s(%d): writing %u bytes\n", __func__, __LINE__,
							size);
		pti_write_to_aperture(mc, kbuf, size);
		n  += size;
		tmp += size;

	} while (len > n);

	kfree(kbuf);
	kbuf = 0;

	return len;
}

const struct tty_operations pti_tty_driver_ops = {
	.open		= pti_tty_driver_open,
	.close		= pti_tty_driver_close,
	.write		= pti_tty_driver_write,
	.write_room	= pti_tty_write_room,
	.install	= pti_tty_install,
	.cleanup	= pti_tty_cleanup
};

const struct file_operations pti_char_driver_ops = {
	.owner		= THIS_MODULE,
	.open		= pti_char_open,
	.release	= pti_char_release,
	.write		= pti_char_write,
};

static struct miscdevice pti_char_driver = {
	.minor		= MISC_DYNAMIC_MINOR,
	.name		= CHARNAME,
	.fops		= &pti_char_driver_ops
};


static void pti_console_write(struct console *c, const char *buf, unsigned len)
{
	static struct masterchannel mc = {.master = CONSOLE_ID, .channel = 0};

	mc.channel = pti_console_channel;
	pti_console_channel = (pti_console_channel + 1) & 0x7f;

	pti_write_full_frame_to_aperture(&mc, buf, len);
}

static struct tty_driver *pti_console_device(struct console *c, int *index)
{
	*index = c->index;
	return pti_tty_driver;
}

static int pti_console_setup(struct console *c, char *opts)
{
	pti_console_channel = 0;
	pti_control_channel = 0;
	return 0;
}

static struct console pti_console = {
	.name		= TTYNAME,
	.write		= pti_console_write,
	.device		= pti_console_device,
	.setup		= pti_console_setup,
	.flags		= CON_PRINTBUFFER | CON_ENABLED,
	.index		= 0,
};

/*
   Note the _probe() call sets everything up and ties the char and tty
   to successfully detecting the PTI device on the pci bus.
*/

static int __devinit pti_pci_probe(struct pci_dev *pdev,
		const struct pci_device_id *ent)
{
	int retval = -EINVAL;
	int pci_bar = 1;

	dev_dbg(&pdev->dev, "%s %s(%d): PTI PCI ID %04x:%04x\n", __FILE__,
			__func__, __LINE__, pdev->vendor, pdev->device);

	retval = pci_enable_device(pdev);
	if (retval != 0) {
		dev_err(&pdev->dev,
			"%s: pci_enable_device() returned error %d\n",
			__func__, retval);
		return retval;
	}

	drv_data = kzalloc(sizeof(*drv_data), GFP_KERNEL);

	if (drv_data == NULL) {
		retval = -ENOMEM;
		dev_err(&pdev->dev,
			"%s(%d): kmalloc() returned NULL memory.\n",
			__func__, __LINE__);
		return retval;
	}
	drv_data->pti_addr = pci_resource_start(pdev, pci_bar);

	retval = pci_request_region(pdev, pci_bar, dev_name(&pdev->dev));
	if (retval != 0) {
		dev_err(&pdev->dev,
			"%s(%d): pci_request_region() returned error %d\n",
			__func__, __LINE__, retval);
		kfree(drv_data);
		return retval;
	}
	drv_data->pti_iolen = pci_resource_len(pdev, pci_bar);
	drv_data->aperture_base = drv_data->pti_addr+APERTURE_14;
	drv_data->pti_ioaddr =
		ioremap_nocache((u32)drv_data->aperture_base,
		APERTURE_LEN);
	if (!drv_data->pti_ioaddr) {
		pci_release_region(pdev, pci_bar);
		retval = -ENOMEM;
		kfree(drv_data);
		return retval;
	}

	pci_set_drvdata(pdev, drv_data);

	tty_port_init(&drv_data->port);
	drv_data->port.ops = &tty_port_ops;

	tty_register_device(pti_tty_driver, 0, NULL);

	register_console(&pti_console);

	retval = misc_register(&pti_char_driver);
	if (retval) {
		pr_err("%s(%d): CHAR registration failed of pti driver\n",
			__func__, __LINE__);
		pr_err("%s(%d): Error value returned: %d\n",
			__func__, __LINE__, retval);
		return retval;
	}

	return retval;
}

static struct pci_driver pti_pci_driver = {
	.name		= PCINAME,
	.id_table	= pci_ids,
	.probe		= pti_pci_probe,
	.remove		= pti_pci_remove,
};

/**
 *
 * pti_init():
 *
 * @return int __init: 0 for success, any other value error.
 *
 */
static int __init pti_init(void)
{
	int retval = -EINVAL;

	/* First register module as tty device */

	pti_tty_driver = alloc_tty_driver(1);
	if (pti_tty_driver == NULL) {
		pr_err("%s(%d): Memory allocation failed for ptiTTY driver\n",
			__func__, __LINE__);
		return -ENOMEM;
	}

	pti_tty_driver->owner			= THIS_MODULE;
	pti_tty_driver->magic			= TTY_DRIVER_MAGIC;
	pti_tty_driver->driver_name		= DRIVERNAME;
	pti_tty_driver->name			= TTYNAME;
	pti_tty_driver->major			= 0;
	pti_tty_driver->minor_start		= 0;
	pti_tty_driver->minor_num		= 1;
	pti_tty_driver->num			= 1;
	pti_tty_driver->type			= TTY_DRIVER_TYPE_SYSTEM;
	pti_tty_driver->subtype			= SYSTEM_TYPE_SYSCONS;
	pti_tty_driver->flags			= TTY_DRIVER_REAL_RAW |
						  TTY_DRIVER_DYNAMIC_DEV;
	pti_tty_driver->init_termios		= tty_std_termios;

	tty_set_operations(pti_tty_driver, &pti_tty_driver_ops);

	retval = tty_register_driver(pti_tty_driver);
	if (retval) {
		pr_err("%s(%d): TTY registration failed of pti driver\n",
			__func__, __LINE__);
		pr_err("%s(%d): Error value returned: %d\n",
			__func__, __LINE__, retval);

		pti_tty_driver = 0;
		return retval;
	}

	retval = pci_register_driver(&pti_pci_driver);

	if (retval) {
		pr_err("%s(%d): PCI registration failed of pti driver\n",
			__func__, __LINE__);
		pr_err("%s(%d): Error value returned: %d\n",
			__func__, __LINE__, retval);

		tty_unregister_driver(pti_tty_driver);
		pr_err("%s(%d): Unregistering TTY part of pti driver\n",
			__func__, __LINE__);
		pti_tty_driver = 0;
		return retval;
	}

	return retval;
}

/**
 * pti_exit(): Unregisters this module as a tty and pci driver.
 */
static void __exit pti_exit(void)
{
	int retval;

	/* If some thead is hanging onto the alloclock, force it to release
	 * it because we are shutting down.
	 */
	if (mutex_is_locked(&alloclock) == 1)
		mutex_unlock(&alloclock);
	mutex_destroy(&alloclock);

	retval = misc_deregister(&pti_char_driver);
	if (retval) {
		pr_err("%s(%d): CHAR unregistration failed of pti driver\n",
			__func__, __LINE__);
		pr_err("%s(%d): Error value returned: %d\n",
			__func__, __LINE__, retval);
	}

	tty_unregister_device(pti_tty_driver, 0);

	retval = tty_unregister_driver(pti_tty_driver);
	if (retval) {
		pr_err("%s(%d): TTY unregistration failed of pti driver\n",
			__func__, __LINE__);
		pr_err("%s(%d): Error value returned: %d\n",
			__func__, __LINE__, retval);
	}

	kfree(drv_data);

	return;
}

module_init(pti_init);
module_exit(pti_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Ken Mills, Jay Freyensee");
MODULE_DESCRIPTION("PTI Driver");