pti.c 27 KB

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