pti.c 26 KB

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