pti.c 27 KB

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