This source file includes following definitions.
- pti_write_to_aperture
- pti_control_frame_built_and_sent
- pti_write_full_frame_to_aperture
- get_id
- pti_request_masterchannel
- pti_release_masterchannel
- pti_writedata
- pti_tty_driver_open
- pti_tty_driver_close
- pti_tty_install
- pti_tty_cleanup
- pti_tty_driver_write
- pti_tty_write_room
- pti_char_open
- pti_char_release
- pti_char_write
- pti_console_write
- pti_console_device
- pti_console_setup
- pti_port_activate
- pti_port_shutdown
- pti_pci_probe
- pti_pci_remove
- pti_init
- pti_exit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 #include <linux/init.h>
17 #include <linux/sched.h>
18 #include <linux/interrupt.h>
19 #include <linux/console.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/tty.h>
23 #include <linux/tty_driver.h>
24 #include <linux/pci.h>
25 #include <linux/mutex.h>
26 #include <linux/miscdevice.h>
27 #include <linux/intel-pti.h>
28 #include <linux/slab.h>
29 #include <linux/uaccess.h>
30
31 #define DRIVERNAME "pti"
32 #define PCINAME "pciPTI"
33 #define TTYNAME "ttyPTI"
34 #define CHARNAME "pti"
35 #define PTITTY_MINOR_START 0
36 #define PTITTY_MINOR_NUM 2
37 #define MAX_APP_IDS 16
38 #define MAX_OS_IDS 16
39 #define MAX_MODEM_IDS 16
40 #define MODEM_BASE_ID 71
41 #define CONTROL_ID 72
42 #define CONSOLE_ID 73
43 #define OS_BASE_ID 74
44 #define APP_BASE_ID 80
45 #define CONTROL_FRAME_LEN 32
46 #define USER_COPY_SIZE 8192
47 #define APERTURE_14 0x3800000
48 #define APERTURE_LEN 0x400000
49
50 struct pti_tty {
51 struct pti_masterchannel *mc;
52 };
53
54 struct pti_dev {
55 struct tty_port port[PTITTY_MINOR_NUM];
56 unsigned long pti_addr;
57 unsigned long aperture_base;
58 void __iomem *pti_ioaddr;
59 u8 ia_app[MAX_APP_IDS];
60 u8 ia_os[MAX_OS_IDS];
61 u8 ia_modem[MAX_MODEM_IDS];
62 };
63
64
65
66
67
68
69 static DEFINE_MUTEX(alloclock);
70
71 static const struct pci_device_id pci_ids[] = {
72 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x82B)},
73 {0}
74 };
75
76 static struct tty_driver *pti_tty_driver;
77 static struct pti_dev *drv_data;
78
79 static unsigned int pti_console_channel;
80 static unsigned int pti_control_channel;
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99 static void pti_write_to_aperture(struct pti_masterchannel *mc,
100 u8 *buf,
101 int len)
102 {
103 int dwordcnt;
104 int final;
105 int i;
106 u32 ptiword;
107 u32 __iomem *aperture;
108 u8 *p = buf;
109
110
111
112
113
114 aperture = drv_data->pti_ioaddr + (mc->master << 15)
115 + (mc->channel << 8);
116
117 dwordcnt = len >> 2;
118 final = len - (dwordcnt << 2);
119 if (final == 0 && dwordcnt != 0) {
120 final += 4;
121 dwordcnt--;
122 }
123
124 for (i = 0; i < dwordcnt; i++) {
125 ptiword = be32_to_cpu(*(u32 *)p);
126 p += 4;
127 iowrite32(ptiword, aperture);
128 }
129
130 aperture += PTI_LASTDWORD_DTS;
131
132 ptiword = 0;
133 for (i = 0; i < final; i++)
134 ptiword |= *p++ << (24-(8*i));
135
136 iowrite32(ptiword, aperture);
137 return;
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 static void pti_control_frame_built_and_sent(struct pti_masterchannel *mc,
158 const char *thread_name)
159 {
160
161
162
163
164 char comm[TASK_COMM_LEN];
165 struct pti_masterchannel mccontrol = {.master = CONTROL_ID,
166 .channel = 0};
167 const char *thread_name_p;
168 const char *control_format = "%3d %3d %s";
169 u8 control_frame[CONTROL_FRAME_LEN];
170
171 if (!thread_name) {
172 if (!in_interrupt())
173 get_task_comm(comm, current);
174 else
175 strncpy(comm, "Interrupt", TASK_COMM_LEN);
176
177
178 comm[TASK_COMM_LEN-1] = 0;
179 thread_name_p = comm;
180 } else {
181 thread_name_p = thread_name;
182 }
183
184 mccontrol.channel = pti_control_channel;
185 pti_control_channel = (pti_control_channel + 1) & 0x7f;
186
187 snprintf(control_frame, CONTROL_FRAME_LEN, control_format, mc->master,
188 mc->channel, thread_name_p);
189 pti_write_to_aperture(&mccontrol, control_frame, strlen(control_frame));
190 }
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206 static void pti_write_full_frame_to_aperture(struct pti_masterchannel *mc,
207 const unsigned char *buf,
208 int len)
209 {
210 pti_control_frame_built_and_sent(mc, NULL);
211 pti_write_to_aperture(mc, (u8 *)buf, len);
212 }
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233 static struct pti_masterchannel *get_id(u8 *id_array,
234 int max_ids,
235 int base_id,
236 const char *thread_name)
237 {
238 struct pti_masterchannel *mc;
239 int i, j, mask;
240
241 mc = kmalloc(sizeof(struct pti_masterchannel), GFP_KERNEL);
242 if (mc == NULL)
243 return NULL;
244
245
246 for (i = 0; i < max_ids; i++)
247 if (id_array[i] != 0xff)
248 break;
249 if (i == max_ids) {
250 kfree(mc);
251 return NULL;
252 }
253
254 mask = 0x80;
255 for (j = 0; j < 8; j++) {
256 if ((id_array[i] & mask) == 0)
257 break;
258 mask >>= 1;
259 }
260
261
262 id_array[i] |= mask;
263 mc->master = base_id;
264 mc->channel = ((i & 0xf)<<3) + j;
265
266 pti_control_frame_built_and_sent(mc, thread_name);
267 return mc;
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 struct pti_masterchannel *pti_request_masterchannel(u8 type,
297 const char *thread_name)
298 {
299 struct pti_masterchannel *mc;
300
301 mutex_lock(&alloclock);
302
303 switch (type) {
304
305 case 0:
306 mc = get_id(drv_data->ia_app, MAX_APP_IDS,
307 APP_BASE_ID, thread_name);
308 break;
309
310 case 1:
311 mc = get_id(drv_data->ia_os, MAX_OS_IDS,
312 OS_BASE_ID, thread_name);
313 break;
314
315 case 2:
316 mc = get_id(drv_data->ia_modem, MAX_MODEM_IDS,
317 MODEM_BASE_ID, thread_name);
318 break;
319 default:
320 mc = NULL;
321 }
322
323 mutex_unlock(&alloclock);
324 return mc;
325 }
326 EXPORT_SYMBOL_GPL(pti_request_masterchannel);
327
328
329
330
331
332
333
334
335
336 void pti_release_masterchannel(struct pti_masterchannel *mc)
337 {
338 u8 master, channel, i;
339
340 mutex_lock(&alloclock);
341
342 if (mc) {
343 master = mc->master;
344 channel = mc->channel;
345
346 if (master == APP_BASE_ID) {
347 i = channel >> 3;
348 drv_data->ia_app[i] &= ~(0x80>>(channel & 0x7));
349 } else if (master == OS_BASE_ID) {
350 i = channel >> 3;
351 drv_data->ia_os[i] &= ~(0x80>>(channel & 0x7));
352 } else {
353 i = channel >> 3;
354 drv_data->ia_modem[i] &= ~(0x80>>(channel & 0x7));
355 }
356
357 kfree(mc);
358 }
359
360 mutex_unlock(&alloclock);
361 }
362 EXPORT_SYMBOL_GPL(pti_release_masterchannel);
363
364
365
366
367
368
369
370
371
372
373
374
375 void pti_writedata(struct pti_masterchannel *mc, u8 *buf, int count)
376 {
377
378
379
380
381
382 if ((mc != NULL) && (buf != NULL) && (count > 0))
383 pti_write_to_aperture(mc, buf, count);
384 return;
385 }
386 EXPORT_SYMBOL_GPL(pti_writedata);
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 static int pti_tty_driver_open(struct tty_struct *tty, struct file *filp)
414 {
415
416
417
418
419
420
421
422 return tty_port_open(tty->port, tty, filp);
423 }
424
425
426
427
428
429
430
431
432
433
434
435
436 static void pti_tty_driver_close(struct tty_struct *tty, struct file *filp)
437 {
438 tty_port_close(tty->port, tty, filp);
439 }
440
441
442
443
444
445
446
447
448
449
450
451
452
453 static int pti_tty_install(struct tty_driver *driver, struct tty_struct *tty)
454 {
455 int idx = tty->index;
456 struct pti_tty *pti_tty_data;
457 int ret = tty_standard_install(driver, tty);
458
459 if (ret == 0) {
460 pti_tty_data = kmalloc(sizeof(struct pti_tty), GFP_KERNEL);
461 if (pti_tty_data == NULL)
462 return -ENOMEM;
463
464 if (idx == PTITTY_MINOR_START)
465 pti_tty_data->mc = pti_request_masterchannel(0, NULL);
466 else
467 pti_tty_data->mc = pti_request_masterchannel(2, NULL);
468
469 if (pti_tty_data->mc == NULL) {
470 kfree(pti_tty_data);
471 return -ENXIO;
472 }
473 tty->driver_data = pti_tty_data;
474 }
475
476 return ret;
477 }
478
479
480
481
482
483
484
485 static void pti_tty_cleanup(struct tty_struct *tty)
486 {
487 struct pti_tty *pti_tty_data = tty->driver_data;
488 if (pti_tty_data == NULL)
489 return;
490 pti_release_masterchannel(pti_tty_data->mc);
491 kfree(pti_tty_data);
492 tty->driver_data = NULL;
493 }
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508 static int pti_tty_driver_write(struct tty_struct *tty,
509 const unsigned char *buf, int len)
510 {
511 struct pti_tty *pti_tty_data = tty->driver_data;
512 if ((pti_tty_data != NULL) && (pti_tty_data->mc != NULL)) {
513 pti_write_to_aperture(pti_tty_data->mc, (u8 *)buf, len);
514 return len;
515 }
516
517
518
519
520 else
521 return -EFAULT;
522 }
523
524
525
526
527
528
529 static int pti_tty_write_room(struct tty_struct *tty)
530 {
531 return 2048;
532 }
533
534
535
536
537
538
539
540
541
542
543
544
545
546 static int pti_char_open(struct inode *inode, struct file *filp)
547 {
548 struct pti_masterchannel *mc;
549
550
551
552
553
554
555
556 mc = pti_request_masterchannel(0, NULL);
557 if (mc == NULL)
558 return -ENOMEM;
559 filp->private_data = mc;
560 return 0;
561 }
562
563
564
565
566
567
568
569
570
571
572
573
574 static int pti_char_release(struct inode *inode, struct file *filp)
575 {
576 pti_release_masterchannel(filp->private_data);
577 filp->private_data = NULL;
578 return 0;
579 }
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601 static ssize_t pti_char_write(struct file *filp, const char __user *data,
602 size_t len, loff_t *ppose)
603 {
604 struct pti_masterchannel *mc;
605 void *kbuf;
606 const char __user *tmp;
607 size_t size = USER_COPY_SIZE;
608 size_t n = 0;
609
610 tmp = data;
611 mc = filp->private_data;
612
613 kbuf = kmalloc(size, GFP_KERNEL);
614 if (kbuf == NULL) {
615 pr_err("%s(%d): buf allocation failed\n",
616 __func__, __LINE__);
617 return -ENOMEM;
618 }
619
620 do {
621 if (len - n > USER_COPY_SIZE)
622 size = USER_COPY_SIZE;
623 else
624 size = len - n;
625
626 if (copy_from_user(kbuf, tmp, size)) {
627 kfree(kbuf);
628 return n ? n : -EFAULT;
629 }
630
631 pti_write_to_aperture(mc, kbuf, size);
632 n += size;
633 tmp += size;
634
635 } while (len > n);
636
637 kfree(kbuf);
638 return len;
639 }
640
641 static const struct tty_operations pti_tty_driver_ops = {
642 .open = pti_tty_driver_open,
643 .close = pti_tty_driver_close,
644 .write = pti_tty_driver_write,
645 .write_room = pti_tty_write_room,
646 .install = pti_tty_install,
647 .cleanup = pti_tty_cleanup
648 };
649
650 static const struct file_operations pti_char_driver_ops = {
651 .owner = THIS_MODULE,
652 .write = pti_char_write,
653 .open = pti_char_open,
654 .release = pti_char_release,
655 };
656
657 static struct miscdevice pti_char_driver = {
658 .minor = MISC_DYNAMIC_MINOR,
659 .name = CHARNAME,
660 .fops = &pti_char_driver_ops
661 };
662
663
664
665
666
667
668
669
670 static void pti_console_write(struct console *c, const char *buf, unsigned len)
671 {
672 static struct pti_masterchannel mc = {.master = CONSOLE_ID,
673 .channel = 0};
674
675 mc.channel = pti_console_channel;
676 pti_console_channel = (pti_console_channel + 1) & 0x7f;
677
678 pti_write_full_frame_to_aperture(&mc, buf, len);
679 }
680
681
682
683
684
685
686
687
688
689
690
691
692 static struct tty_driver *pti_console_device(struct console *c, int *index)
693 {
694 *index = c->index;
695 return pti_tty_driver;
696 }
697
698
699
700
701
702
703
704
705
706
707 static int pti_console_setup(struct console *c, char *opts)
708 {
709 pti_console_channel = 0;
710 pti_control_channel = 0;
711 return 0;
712 }
713
714
715
716
717
718
719
720
721
722
723
724 static struct console pti_console = {
725 .name = TTYNAME,
726 .write = pti_console_write,
727 .device = pti_console_device,
728 .setup = pti_console_setup,
729 .flags = CON_PRINTBUFFER,
730 .index = 0,
731 };
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747 static int pti_port_activate(struct tty_port *port, struct tty_struct *tty)
748 {
749 if (port->tty->index == PTITTY_MINOR_START)
750 console_start(&pti_console);
751 return 0;
752 }
753
754
755
756
757
758
759
760
761
762
763
764 static void pti_port_shutdown(struct tty_port *port)
765 {
766 if (port->tty->index == PTITTY_MINOR_START)
767 console_stop(&pti_console);
768 }
769
770 static const struct tty_port_operations tty_port_ops = {
771 .activate = pti_port_activate,
772 .shutdown = pti_port_shutdown,
773 };
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791 static int pti_pci_probe(struct pci_dev *pdev,
792 const struct pci_device_id *ent)
793 {
794 unsigned int a;
795 int retval = -EINVAL;
796 int pci_bar = 1;
797
798 dev_dbg(&pdev->dev, "%s %s(%d): PTI PCI ID %04x:%04x\n", __FILE__,
799 __func__, __LINE__, pdev->vendor, pdev->device);
800
801 retval = misc_register(&pti_char_driver);
802 if (retval) {
803 pr_err("%s(%d): CHAR registration failed of pti driver\n",
804 __func__, __LINE__);
805 pr_err("%s(%d): Error value returned: %d\n",
806 __func__, __LINE__, retval);
807 goto err;
808 }
809
810 retval = pci_enable_device(pdev);
811 if (retval != 0) {
812 dev_err(&pdev->dev,
813 "%s: pci_enable_device() returned error %d\n",
814 __func__, retval);
815 goto err_unreg_misc;
816 }
817
818 drv_data = kzalloc(sizeof(*drv_data), GFP_KERNEL);
819 if (drv_data == NULL) {
820 retval = -ENOMEM;
821 dev_err(&pdev->dev,
822 "%s(%d): kmalloc() returned NULL memory.\n",
823 __func__, __LINE__);
824 goto err_disable_pci;
825 }
826 drv_data->pti_addr = pci_resource_start(pdev, pci_bar);
827
828 retval = pci_request_region(pdev, pci_bar, dev_name(&pdev->dev));
829 if (retval != 0) {
830 dev_err(&pdev->dev,
831 "%s(%d): pci_request_region() returned error %d\n",
832 __func__, __LINE__, retval);
833 goto err_free_dd;
834 }
835 drv_data->aperture_base = drv_data->pti_addr+APERTURE_14;
836 drv_data->pti_ioaddr =
837 ioremap_nocache((u32)drv_data->aperture_base,
838 APERTURE_LEN);
839 if (!drv_data->pti_ioaddr) {
840 retval = -ENOMEM;
841 goto err_rel_reg;
842 }
843
844 pci_set_drvdata(pdev, drv_data);
845
846 for (a = 0; a < PTITTY_MINOR_NUM; a++) {
847 struct tty_port *port = &drv_data->port[a];
848 tty_port_init(port);
849 port->ops = &tty_port_ops;
850
851 tty_port_register_device(port, pti_tty_driver, a, &pdev->dev);
852 }
853
854 register_console(&pti_console);
855
856 return 0;
857 err_rel_reg:
858 pci_release_region(pdev, pci_bar);
859 err_free_dd:
860 kfree(drv_data);
861 err_disable_pci:
862 pci_disable_device(pdev);
863 err_unreg_misc:
864 misc_deregister(&pti_char_driver);
865 err:
866 return retval;
867 }
868
869
870
871
872
873
874 static void pti_pci_remove(struct pci_dev *pdev)
875 {
876 struct pti_dev *drv_data = pci_get_drvdata(pdev);
877 unsigned int a;
878
879 unregister_console(&pti_console);
880
881 for (a = 0; a < PTITTY_MINOR_NUM; a++) {
882 tty_unregister_device(pti_tty_driver, a);
883 tty_port_destroy(&drv_data->port[a]);
884 }
885
886 iounmap(drv_data->pti_ioaddr);
887 kfree(drv_data);
888 pci_release_region(pdev, 1);
889 pci_disable_device(pdev);
890
891 misc_deregister(&pti_char_driver);
892 }
893
894 static struct pci_driver pti_pci_driver = {
895 .name = PCINAME,
896 .id_table = pci_ids,
897 .probe = pti_pci_probe,
898 .remove = pti_pci_remove,
899 };
900
901
902
903
904
905
906
907
908
909
910
911 static int __init pti_init(void)
912 {
913 int retval = -EINVAL;
914
915
916
917 pti_tty_driver = alloc_tty_driver(PTITTY_MINOR_NUM);
918 if (pti_tty_driver == NULL) {
919 pr_err("%s(%d): Memory allocation failed for ptiTTY driver\n",
920 __func__, __LINE__);
921 return -ENOMEM;
922 }
923
924 pti_tty_driver->driver_name = DRIVERNAME;
925 pti_tty_driver->name = TTYNAME;
926 pti_tty_driver->major = 0;
927 pti_tty_driver->minor_start = PTITTY_MINOR_START;
928 pti_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
929 pti_tty_driver->subtype = SYSTEM_TYPE_SYSCONS;
930 pti_tty_driver->flags = TTY_DRIVER_REAL_RAW |
931 TTY_DRIVER_DYNAMIC_DEV;
932 pti_tty_driver->init_termios = tty_std_termios;
933
934 tty_set_operations(pti_tty_driver, &pti_tty_driver_ops);
935
936 retval = tty_register_driver(pti_tty_driver);
937 if (retval) {
938 pr_err("%s(%d): TTY registration failed of pti driver\n",
939 __func__, __LINE__);
940 pr_err("%s(%d): Error value returned: %d\n",
941 __func__, __LINE__, retval);
942
943 goto put_tty;
944 }
945
946 retval = pci_register_driver(&pti_pci_driver);
947 if (retval) {
948 pr_err("%s(%d): PCI registration failed of pti driver\n",
949 __func__, __LINE__);
950 pr_err("%s(%d): Error value returned: %d\n",
951 __func__, __LINE__, retval);
952 goto unreg_tty;
953 }
954
955 return 0;
956 unreg_tty:
957 tty_unregister_driver(pti_tty_driver);
958 put_tty:
959 put_tty_driver(pti_tty_driver);
960 pti_tty_driver = NULL;
961 return retval;
962 }
963
964
965
966
967 static void __exit pti_exit(void)
968 {
969 tty_unregister_driver(pti_tty_driver);
970 pci_unregister_driver(&pti_pci_driver);
971 put_tty_driver(pti_tty_driver);
972 }
973
974 module_init(pti_init);
975 module_exit(pti_exit);
976
977 MODULE_LICENSE("GPL");
978 MODULE_AUTHOR("Ken Mills, Jay Freyensee");
979 MODULE_DESCRIPTION("PTI Driver");
980