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