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