pti.c 26 KB

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