pti.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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. This
  285. * will de-allocate the structure via kfree().
  286. */
  287. void pti_release_masterchannel(struct pti_masterchannel *mc)
  288. {
  289. u8 master, channel, i;
  290. mutex_lock(&alloclock);
  291. if (mc) {
  292. master = mc->master;
  293. channel = mc->channel;
  294. if (master == APP_BASE_ID) {
  295. i = channel >> 3;
  296. drv_data->ia_app[i] &= ~(0x80>>(channel & 0x7));
  297. } else if (master == OS_BASE_ID) {
  298. i = channel >> 3;
  299. drv_data->ia_os[i] &= ~(0x80>>(channel & 0x7));
  300. } else {
  301. i = channel >> 3;
  302. drv_data->ia_modem[i] &= ~(0x80>>(channel & 0x7));
  303. }
  304. kfree(mc);
  305. }
  306. mutex_unlock(&alloclock);
  307. }
  308. EXPORT_SYMBOL_GPL(pti_release_masterchannel);
  309. /**
  310. * pti_writedata()- Kernel API function used to write trace
  311. * debugging data to PTI HW.
  312. *
  313. * @mc: Master, channel aperture ID address to write to.
  314. * Null value will return with no write occurring.
  315. * @buf: Trace debuging data to write to the PTI HW.
  316. * Null value will return with no write occurring.
  317. * @count: Size of buf. Value of 0 or a negative number will
  318. * return with no write occuring.
  319. */
  320. void pti_writedata(struct pti_masterchannel *mc, u8 *buf, int count)
  321. {
  322. /*
  323. * since this function is exported, this is treated like an
  324. * API function, thus, all parameters should
  325. * be checked for validity.
  326. */
  327. if ((mc != NULL) && (buf != NULL) && (count > 0))
  328. pti_write_to_aperture(mc, buf, count);
  329. return;
  330. }
  331. EXPORT_SYMBOL_GPL(pti_writedata);
  332. /**
  333. * pti_pci_remove()- Driver exit method to remove PTI from
  334. * PCI bus.
  335. * @pdev: variable containing pci info of PTI.
  336. */
  337. static void __devexit pti_pci_remove(struct pci_dev *pdev)
  338. {
  339. struct pti_dev *drv_data;
  340. drv_data = pci_get_drvdata(pdev);
  341. if (drv_data != NULL) {
  342. pci_iounmap(pdev, drv_data->pti_ioaddr);
  343. pci_set_drvdata(pdev, NULL);
  344. kfree(drv_data);
  345. pci_release_region(pdev, 1);
  346. pci_disable_device(pdev);
  347. }
  348. }
  349. /*
  350. * for the tty_driver_*() basic function descriptions, see tty_driver.h.
  351. * Specific header comments made for PTI-related specifics.
  352. */
  353. /**
  354. * pti_tty_driver_open()- Open an Application master, channel aperture
  355. * ID to the PTI device via tty device.
  356. *
  357. * @tty: tty interface.
  358. * @filp: filp interface pased to tty_port_open() call.
  359. *
  360. * Returns:
  361. * int, 0 for success
  362. * otherwise, fail value
  363. *
  364. * The main purpose of using the tty device interface is for
  365. * each tty port to have a unique PTI write aperture. In an
  366. * example use case, ttyPTI0 gets syslogd and an APP aperture
  367. * ID and ttyPTI1 is where the n_tracesink ldisc hooks to route
  368. * modem messages into PTI. Modem trace data does not have to
  369. * go to ttyPTI1, but ttyPTI0 and ttyPTI1 do need to be distinct
  370. * master IDs. These messages go through the PTI HW and out of
  371. * the handheld platform and to the Fido/Lauterbach device.
  372. */
  373. static int pti_tty_driver_open(struct tty_struct *tty, struct file *filp)
  374. {
  375. /*
  376. * we actually want to allocate a new channel per open, per
  377. * system arch. HW gives more than plenty channels for a single
  378. * system task to have its own channel to write trace data. This
  379. * also removes a locking requirement for the actual write
  380. * procedure.
  381. */
  382. return tty_port_open(&drv_data->port, tty, filp);
  383. }
  384. /**
  385. * pti_tty_driver_close()- close tty device and release Application
  386. * master, channel aperture ID to the PTI device via tty device.
  387. *
  388. * @tty: tty interface.
  389. * @filp: filp interface pased to tty_port_close() call.
  390. *
  391. * The main purpose of using the tty device interface is to route
  392. * syslog daemon messages to the PTI HW and out of the handheld platform
  393. * and to the Fido/Lauterbach device.
  394. */
  395. static void pti_tty_driver_close(struct tty_struct *tty, struct file *filp)
  396. {
  397. tty_port_close(&drv_data->port, tty, filp);
  398. }
  399. /**
  400. * pti_tty_intstall()- Used to set up specific master-channels
  401. * to tty ports for organizational purposes when
  402. * tracing viewed from debuging tools.
  403. *
  404. * @driver: tty driver information.
  405. * @tty: tty struct containing pti information.
  406. *
  407. * Returns:
  408. * 0 for success
  409. * otherwise, error
  410. */
  411. static int pti_tty_install(struct tty_driver *driver, struct tty_struct *tty)
  412. {
  413. int idx = tty->index;
  414. struct pti_tty *pti_tty_data;
  415. int ret = tty_init_termios(tty);
  416. if (ret == 0) {
  417. tty_driver_kref_get(driver);
  418. tty->count++;
  419. driver->ttys[idx] = tty;
  420. pti_tty_data = kmalloc(sizeof(struct pti_tty), GFP_KERNEL);
  421. if (pti_tty_data == NULL)
  422. return -ENOMEM;
  423. if (idx == PTITTY_MINOR_START)
  424. pti_tty_data->mc = pti_request_masterchannel(0);
  425. else
  426. pti_tty_data->mc = pti_request_masterchannel(2);
  427. if (pti_tty_data->mc == NULL) {
  428. kfree(pti_tty_data);
  429. return -ENXIO;
  430. }
  431. tty->driver_data = pti_tty_data;
  432. }
  433. return ret;
  434. }
  435. /**
  436. * pti_tty_cleanup()- Used to de-allocate master-channel resources
  437. * tied to tty's of this driver.
  438. *
  439. * @tty: tty struct containing pti information.
  440. */
  441. static void pti_tty_cleanup(struct tty_struct *tty)
  442. {
  443. struct pti_tty *pti_tty_data = tty->driver_data;
  444. if (pti_tty_data == NULL)
  445. return;
  446. pti_release_masterchannel(pti_tty_data->mc);
  447. kfree(pti_tty_data);
  448. tty->driver_data = NULL;
  449. }
  450. /**
  451. * pti_tty_driver_write()- Write trace debugging data through the char
  452. * interface to the PTI HW. Part of the misc device implementation.
  453. *
  454. * @filp: Contains private data which is used to obtain
  455. * master, channel write ID.
  456. * @data: trace data to be written.
  457. * @len: # of byte to write.
  458. *
  459. * Returns:
  460. * int, # of bytes written
  461. * otherwise, error
  462. */
  463. static int pti_tty_driver_write(struct tty_struct *tty,
  464. const unsigned char *buf, int len)
  465. {
  466. struct pti_tty *pti_tty_data = tty->driver_data;
  467. if ((pti_tty_data != NULL) && (pti_tty_data->mc != NULL)) {
  468. pti_write_to_aperture(pti_tty_data->mc, (u8 *)buf, len);
  469. return len;
  470. }
  471. /*
  472. * we can't write to the pti hardware if the private driver_data
  473. * and the mc address is not there.
  474. */
  475. else
  476. return -EFAULT;
  477. }
  478. /**
  479. * pti_tty_write_room()- Always returns 2048.
  480. *
  481. * @tty: contains tty info of the pti driver.
  482. */
  483. static int pti_tty_write_room(struct tty_struct *tty)
  484. {
  485. return 2048;
  486. }
  487. /**
  488. * pti_char_open()- Open an Application master, channel aperture
  489. * ID to the PTI device. Part of the misc device implementation.
  490. *
  491. * @inode: not used.
  492. * @filp: Output- will have a masterchannel struct set containing
  493. * the allocated application PTI aperture write address.
  494. *
  495. * Returns:
  496. * int, 0 for success
  497. * otherwise, a fail value
  498. */
  499. static int pti_char_open(struct inode *inode, struct file *filp)
  500. {
  501. struct pti_masterchannel *mc;
  502. /*
  503. * We really do want to fail immediately if
  504. * pti_request_masterchannel() fails,
  505. * before assigning the value to filp->private_data.
  506. * Slightly easier to debug if this driver needs debugging.
  507. */
  508. mc = pti_request_masterchannel(0);
  509. if (mc == NULL)
  510. return -ENOMEM;
  511. filp->private_data = mc;
  512. return 0;
  513. }
  514. /**
  515. * pti_char_release()- Close a char channel to the PTI device. Part
  516. * of the misc device implementation.
  517. *
  518. * @inode: Not used in this implementaiton.
  519. * @filp: Contains private_data that contains the master, channel
  520. * ID to be released by the PTI device.
  521. *
  522. * Returns:
  523. * always 0
  524. */
  525. static int pti_char_release(struct inode *inode, struct file *filp)
  526. {
  527. pti_release_masterchannel(filp->private_data);
  528. filp->private_data = NULL;
  529. return 0;
  530. }
  531. /**
  532. * pti_char_write()- Write trace debugging data through the char
  533. * interface to the PTI HW. Part of the misc device implementation.
  534. *
  535. * @filp: Contains private data which is used to obtain
  536. * master, channel write ID.
  537. * @data: trace data to be written.
  538. * @len: # of byte to write.
  539. * @ppose: Not used in this function implementation.
  540. *
  541. * Returns:
  542. * int, # of bytes written
  543. * otherwise, error value
  544. *
  545. * Notes: From side discussions with Alan Cox and experimenting
  546. * with PTI debug HW like Nokia's Fido box and Lauterbach
  547. * devices, 8192 byte write buffer used by USER_COPY_SIZE was
  548. * deemed an appropriate size for this type of usage with
  549. * debugging HW.
  550. */
  551. static ssize_t pti_char_write(struct file *filp, const char __user *data,
  552. size_t len, loff_t *ppose)
  553. {
  554. struct pti_masterchannel *mc;
  555. void *kbuf;
  556. const char __user *tmp;
  557. size_t size = USER_COPY_SIZE;
  558. size_t n = 0;
  559. tmp = data;
  560. mc = filp->private_data;
  561. kbuf = kmalloc(size, GFP_KERNEL);
  562. if (kbuf == NULL) {
  563. pr_err("%s(%d): buf allocation failed\n",
  564. __func__, __LINE__);
  565. return -ENOMEM;
  566. }
  567. do {
  568. if (len - n > USER_COPY_SIZE)
  569. size = USER_COPY_SIZE;
  570. else
  571. size = len - n;
  572. if (copy_from_user(kbuf, tmp, size)) {
  573. kfree(kbuf);
  574. return n ? n : -EFAULT;
  575. }
  576. pti_write_to_aperture(mc, kbuf, size);
  577. n += size;
  578. tmp += size;
  579. } while (len > n);
  580. kfree(kbuf);
  581. return len;
  582. }
  583. static const struct tty_operations pti_tty_driver_ops = {
  584. .open = pti_tty_driver_open,
  585. .close = pti_tty_driver_close,
  586. .write = pti_tty_driver_write,
  587. .write_room = pti_tty_write_room,
  588. .install = pti_tty_install,
  589. .cleanup = pti_tty_cleanup
  590. };
  591. static const struct file_operations pti_char_driver_ops = {
  592. .owner = THIS_MODULE,
  593. .write = pti_char_write,
  594. .open = pti_char_open,
  595. .release = pti_char_release,
  596. };
  597. static struct miscdevice pti_char_driver = {
  598. .minor = MISC_DYNAMIC_MINOR,
  599. .name = CHARNAME,
  600. .fops = &pti_char_driver_ops
  601. };
  602. /**
  603. * pti_console_write()- Write to the console that has been acquired.
  604. *
  605. * @c: Not used in this implementaiton.
  606. * @buf: Data to be written.
  607. * @len: Length of buf.
  608. */
  609. static void pti_console_write(struct console *c, const char *buf, unsigned len)
  610. {
  611. static struct pti_masterchannel mc = {.master = CONSOLE_ID,
  612. .channel = 0};
  613. mc.channel = pti_console_channel;
  614. pti_console_channel = (pti_console_channel + 1) & 0x7f;
  615. pti_write_full_frame_to_aperture(&mc, buf, len);
  616. }
  617. /**
  618. * pti_console_device()- Return the driver tty structure and set the
  619. * associated index implementation.
  620. *
  621. * @c: Console device of the driver.
  622. * @index: index associated with c.
  623. *
  624. * Returns:
  625. * always value of pti_tty_driver structure when this function
  626. * is called.
  627. */
  628. static struct tty_driver *pti_console_device(struct console *c, int *index)
  629. {
  630. *index = c->index;
  631. return pti_tty_driver;
  632. }
  633. /**
  634. * pti_console_setup()- Initialize console variables used by the driver.
  635. *
  636. * @c: Not used.
  637. * @opts: Not used.
  638. *
  639. * Returns:
  640. * always 0.
  641. */
  642. static int pti_console_setup(struct console *c, char *opts)
  643. {
  644. pti_console_channel = 0;
  645. pti_control_channel = 0;
  646. return 0;
  647. }
  648. /*
  649. * pti_console struct, used to capture OS printk()'s and shift
  650. * out to the PTI device for debugging. This cannot be
  651. * enabled upon boot because of the possibility of eating
  652. * any serial console printk's (race condition discovered).
  653. * The console should be enabled upon when the tty port is
  654. * used for the first time. Since the primary purpose for
  655. * the tty port is to hook up syslog to it, the tty port
  656. * will be open for a really long time.
  657. */
  658. static struct console pti_console = {
  659. .name = TTYNAME,
  660. .write = pti_console_write,
  661. .device = pti_console_device,
  662. .setup = pti_console_setup,
  663. .flags = CON_PRINTBUFFER,
  664. .index = 0,
  665. };
  666. /**
  667. * pti_port_activate()- Used to start/initialize any items upon
  668. * first opening of tty_port().
  669. *
  670. * @port- The tty port number of the PTI device.
  671. * @tty- The tty struct associated with this device.
  672. *
  673. * Returns:
  674. * always returns 0
  675. *
  676. * Notes: The primary purpose of the PTI tty port 0 is to hook
  677. * the syslog daemon to it; thus this port will be open for a
  678. * very long time.
  679. */
  680. static int pti_port_activate(struct tty_port *port, struct tty_struct *tty)
  681. {
  682. if (port->tty->index == PTITTY_MINOR_START)
  683. console_start(&pti_console);
  684. return 0;
  685. }
  686. /**
  687. * pti_port_shutdown()- Used to stop/shutdown any items upon the
  688. * last tty port close.
  689. *
  690. * @port- The tty port number of the PTI device.
  691. *
  692. * Notes: The primary purpose of the PTI tty port 0 is to hook
  693. * the syslog daemon to it; thus this port will be open for a
  694. * very long time.
  695. */
  696. static void pti_port_shutdown(struct tty_port *port)
  697. {
  698. if (port->tty->index == PTITTY_MINOR_START)
  699. console_stop(&pti_console);
  700. }
  701. static const struct tty_port_operations tty_port_ops = {
  702. .activate = pti_port_activate,
  703. .shutdown = pti_port_shutdown,
  704. };
  705. /*
  706. * Note the _probe() call sets everything up and ties the char and tty
  707. * to successfully detecting the PTI device on the pci bus.
  708. */
  709. /**
  710. * pti_pci_probe()- Used to detect pti on the pci bus and set
  711. * things up in the driver.
  712. *
  713. * @pdev- pci_dev struct values for pti.
  714. * @ent- pci_device_id struct for pti driver.
  715. *
  716. * Returns:
  717. * 0 for success
  718. * otherwise, error
  719. */
  720. static int __devinit pti_pci_probe(struct pci_dev *pdev,
  721. const struct pci_device_id *ent)
  722. {
  723. int retval = -EINVAL;
  724. int pci_bar = 1;
  725. dev_dbg(&pdev->dev, "%s %s(%d): PTI PCI ID %04x:%04x\n", __FILE__,
  726. __func__, __LINE__, pdev->vendor, pdev->device);
  727. retval = misc_register(&pti_char_driver);
  728. if (retval) {
  729. pr_err("%s(%d): CHAR registration failed of pti driver\n",
  730. __func__, __LINE__);
  731. pr_err("%s(%d): Error value returned: %d\n",
  732. __func__, __LINE__, retval);
  733. return retval;
  734. }
  735. retval = pci_enable_device(pdev);
  736. if (retval != 0) {
  737. dev_err(&pdev->dev,
  738. "%s: pci_enable_device() returned error %d\n",
  739. __func__, retval);
  740. return retval;
  741. }
  742. drv_data = kzalloc(sizeof(*drv_data), GFP_KERNEL);
  743. if (drv_data == NULL) {
  744. retval = -ENOMEM;
  745. dev_err(&pdev->dev,
  746. "%s(%d): kmalloc() returned NULL memory.\n",
  747. __func__, __LINE__);
  748. return retval;
  749. }
  750. drv_data->pti_addr = pci_resource_start(pdev, pci_bar);
  751. retval = pci_request_region(pdev, pci_bar, dev_name(&pdev->dev));
  752. if (retval != 0) {
  753. dev_err(&pdev->dev,
  754. "%s(%d): pci_request_region() returned error %d\n",
  755. __func__, __LINE__, retval);
  756. kfree(drv_data);
  757. return retval;
  758. }
  759. drv_data->aperture_base = drv_data->pti_addr+APERTURE_14;
  760. drv_data->pti_ioaddr =
  761. ioremap_nocache((u32)drv_data->aperture_base,
  762. APERTURE_LEN);
  763. if (!drv_data->pti_ioaddr) {
  764. pci_release_region(pdev, pci_bar);
  765. retval = -ENOMEM;
  766. kfree(drv_data);
  767. return retval;
  768. }
  769. pci_set_drvdata(pdev, drv_data);
  770. tty_port_init(&drv_data->port);
  771. drv_data->port.ops = &tty_port_ops;
  772. tty_register_device(pti_tty_driver, 0, &pdev->dev);
  773. tty_register_device(pti_tty_driver, 1, &pdev->dev);
  774. register_console(&pti_console);
  775. return retval;
  776. }
  777. static struct pci_driver pti_pci_driver = {
  778. .name = PCINAME,
  779. .id_table = pci_ids,
  780. .probe = pti_pci_probe,
  781. .remove = pti_pci_remove,
  782. };
  783. /**
  784. *
  785. * pti_init()- Overall entry/init call to the pti driver.
  786. * It starts the registration process with the kernel.
  787. *
  788. * Returns:
  789. * int __init, 0 for success
  790. * otherwise value is an error
  791. *
  792. */
  793. static int __init pti_init(void)
  794. {
  795. int retval = -EINVAL;
  796. /* First register module as tty device */
  797. pti_tty_driver = alloc_tty_driver(1);
  798. if (pti_tty_driver == NULL) {
  799. pr_err("%s(%d): Memory allocation failed for ptiTTY driver\n",
  800. __func__, __LINE__);
  801. return -ENOMEM;
  802. }
  803. pti_tty_driver->owner = THIS_MODULE;
  804. pti_tty_driver->magic = TTY_DRIVER_MAGIC;
  805. pti_tty_driver->driver_name = DRIVERNAME;
  806. pti_tty_driver->name = TTYNAME;
  807. pti_tty_driver->major = 0;
  808. pti_tty_driver->minor_start = PTITTY_MINOR_START;
  809. pti_tty_driver->minor_num = PTITTY_MINOR_NUM;
  810. pti_tty_driver->num = PTITTY_MINOR_NUM;
  811. pti_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
  812. pti_tty_driver->subtype = SYSTEM_TYPE_SYSCONS;
  813. pti_tty_driver->flags = TTY_DRIVER_REAL_RAW |
  814. TTY_DRIVER_DYNAMIC_DEV;
  815. pti_tty_driver->init_termios = tty_std_termios;
  816. tty_set_operations(pti_tty_driver, &pti_tty_driver_ops);
  817. retval = tty_register_driver(pti_tty_driver);
  818. if (retval) {
  819. pr_err("%s(%d): TTY registration failed of pti driver\n",
  820. __func__, __LINE__);
  821. pr_err("%s(%d): Error value returned: %d\n",
  822. __func__, __LINE__, retval);
  823. pti_tty_driver = NULL;
  824. return retval;
  825. }
  826. retval = pci_register_driver(&pti_pci_driver);
  827. if (retval) {
  828. pr_err("%s(%d): PCI registration failed of pti driver\n",
  829. __func__, __LINE__);
  830. pr_err("%s(%d): Error value returned: %d\n",
  831. __func__, __LINE__, retval);
  832. tty_unregister_driver(pti_tty_driver);
  833. pr_err("%s(%d): Unregistering TTY part of pti driver\n",
  834. __func__, __LINE__);
  835. pti_tty_driver = NULL;
  836. return retval;
  837. }
  838. return retval;
  839. }
  840. /**
  841. * pti_exit()- Unregisters this module as a tty and pci driver.
  842. */
  843. static void __exit pti_exit(void)
  844. {
  845. int retval;
  846. tty_unregister_device(pti_tty_driver, 0);
  847. tty_unregister_device(pti_tty_driver, 1);
  848. retval = tty_unregister_driver(pti_tty_driver);
  849. if (retval) {
  850. pr_err("%s(%d): TTY unregistration failed of pti driver\n",
  851. __func__, __LINE__);
  852. pr_err("%s(%d): Error value returned: %d\n",
  853. __func__, __LINE__, retval);
  854. }
  855. pci_unregister_driver(&pti_pci_driver);
  856. retval = misc_deregister(&pti_char_driver);
  857. if (retval) {
  858. pr_err("%s(%d): CHAR unregistration failed of pti driver\n",
  859. __func__, __LINE__);
  860. pr_err("%s(%d): Error value returned: %d\n",
  861. __func__, __LINE__, retval);
  862. }
  863. unregister_console(&pti_console);
  864. return;
  865. }
  866. module_init(pti_init);
  867. module_exit(pti_exit);
  868. MODULE_LICENSE("GPL");
  869. MODULE_AUTHOR("Ken Mills, Jay Freyensee");
  870. MODULE_DESCRIPTION("PTI Driver");