pti.c 27 KB

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