pti.c 26 KB

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