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