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