pti.c 26 KB

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