From mboxrd@z Thu Jan 1 00:00:00 1970 From: testlaster@gmail.com (testlaster) Date: Wed, 10 Dec 2014 09:33:58 +0200 Subject: cant figure out this PCI driver Message-ID: <5487F766.4090900@gmail.com> To: kernelnewbies@lists.kernelnewbies.org List-Id: kernelnewbies.lists.kernelnewbies.org Hi guys. Im first going to explain why im doing this and what I want to achieve before giving the problem. My main objective here is to gain access to a piece of shared memory. The memory is shared between an FPGA and a PCI chip called a PLX9054. I have to work through the PCI chip to gain access to the RAM.The only way I know of In order to do that is to build a driver PCI for Linux (Lubuntu). Now Ive looked at a couple of examples and I have taken one of the examples and changed it quite allot. It seems like I am attaching to the rite device because when I check the VendorId and DeviceID they seem to be the right ones. For now im debugging in the .write() function just to see if Im heading in the right direction at least "Latching on to RAM".Im doing that because now I can see all my functions whenever I use " echo 'write' > pciproto " where pciproto is the DEVICE file in /dev/ . But I am not having any luck though. Ive been reading through /linux/pci.h , /source/driver/pci/pci.c , /source/driver/dma/dma.c and allot of other source files and headers looking for something that can help me but what I have found doesn't seem to work. Ive tried DMA but checking DMA capability with pci_set_dma_mask() returned a FAIL as well as pci_request_region(). Im not quite sure what im suppose to use or to do to just be able to latch onto the piece of RAM. If im going towards this all wrong please let me know and also let me know if theres any better way to do this. Here is my CODE: [CODE] 1. /** 2. * This program is an attempt to latch on to the FG memory that is shared with the FPGA 3. * After a data sample has been taken the fg stores the data in this region. 4. * This Driver will latch on to the DEV file named pciproto. For testing purposes the /dev/pciproto has to be 5. *created manually in the /dev folder. The following command will create a DEV file with a maximum number of 6. *250 and a minor number of 0: mknod pciproto c 250 0 7. * 8. * There are only 3 registers: 9. * STATUS: addr = BAR2 + 0 10. * unused for now 11. * READ_DATA: addr = BAR2 + 1 12. * this register can only be read. Each time it's read, the device 13. * read a byte from his input and return it. 14. * WRITE_DATA: addr = BAR2 + 2 15. * this register can only be written. Each time it's written, the device 16. * write the byte on his output. 17. * 18. */ 19. 20. #include 21. #include 22. #include 23. #include 24. #include 25. #include /* for cdev_ */ 26. #include /* for put_user */ 27. #include 28. 29. #define MAX_DEVICE 8 30. #define DEVICE_NAME "pciproto" 31. #define BAR_IO 2 32. #define BAR_MEM 3 33. #define BAR_SETTINGS 2 34. #define BAR_PHOTO_MEM 3 35. 36. 37. MODULE_DESCRIPTION("Test PCI driver"); 38. MODULE_AUTHOR("Gerfg "); 39. MODULE_LICENSE("GPL"); 40. 41. /** 42. * This table holds the list of (VendorID,DeviceID) supported by this driver 43. * 44. */ 45. staticstructpci_device_id pci_ids[]={ 46. {PCI_DEVICE(0x10B5,0x9054),}, 47. {0,} 48. }; 49. 50. /** 51. * This macro ... 52. * 53. */ 54. MODULE_DEVICE_TABLE(pci,pci_ids); 55. 56. staticdev_t devno; 57. staticintmajor; 58. 59. /** 60. * This structure is used to link a pci_dev to a cdev 61. * 62. */ 63. structpci_cdev{ 64. intminor; 65. structpci_dev*pci_dev; 66. structcdev*cdev; 67. }; 68. 69. staticstructpci_cdev pci_cdev[MAX_DEVICE]; 70. 71. /* this function initialize the table with all struct pci_cdev */ 72. staticvoidpci_cdev_init(structpci_cdev pci_cdev[],intsize,intfirst_minor) 73. { 74. inti; 75. 76. for(i=0;i failed 86. others => succes 87. */ 88. staticintpci_cdev_add(structpci_cdev pci_cdev[],intsize,structpci_dev*pdev) 89. { 90. inti,res=-1; 91. 92. for(i=0;i not found 149. others => found 150. */ 151. staticintpci_cdev_search_minor(structpci_cdev pci_cdev[], 152. intsize,structpci_dev*pdev) 153. { 154. inti,minor=-1; 155. 156. for(i=0;iprivate_data=(void*)pci_cdev_search_pci_dev(pci_cdev,MAX_DEVICE,minor); 176. printk(KERN_INFO"RAN: pci_open()\n"); 177. return0; 178. } 179. 180. /** 181. * This function is called when the device node is closed 182. * 183. */ 184. staticintpci_release(structinode*inode,structfile*file) 185. { 186. return0; 187. } 188. 189. /** 190. * This function is called when the device node is read 191. * 192. */ 193. staticssize_t pci_read(structfile*file, /* see include/linux/fs.h */ 194. char*buffer, /* buffer to fill with data */ 195. size_tlength,/* length of the buffer */ 196. loff_t*offset) 197. { 198. intbyte_read=0; 199. unsignedcharvalue; 200. structpci_dev*pdev=(structpci_dev*)file->private_data; 201. unsignedlongpci_io_addr=0; 202. 203. pci_io_addr=pci_resource_start(pdev,BAR_IO); 204. 205. while(byte_readprivate_data; 226. unsignedlongpci_reg_mem_addr=0lu; 227. unsignedlongpci_photo_mem_addr=0lu; 228. unsignedintdma_mask=1; 229. inttest=2; 230. constchar*MemRegionName=NULL; 231. //uint32_t *NEW; 232. //MemRegionName = (const char*)malloc(sizeof(const char)); 233. printk(KERN_INFO"RAN: pci_write()\n"); 234. pci_reg_mem_addr=pci_resource_start(pdev,BAR_SETTINGS); 235. pci_photo_mem_addr=pci_resource_start(pdev,BAR_PHOTO_MEM); 236. printk(KERN_INFO"Device VendorID = %x , DeviceID = %x",pdev->vendor,pdev->device); 237. printk(KERN_INFO"DATA WRITTEN TO DEVICE:"); 238. for(i=0;i this driver don't handle this device 303. * 1 => this driver handle this device 304. * 305. */ 306. staticintpci_probe(structpci_dev*dev,conststructpci_device_id*id) 307. { 308. intret,minor; 309. structcdev*cdev; 310. dev_t devno; 311. 312. /* add this pci device in pci_cdev */ 313. if((minor=pci_cdev_add(pci_cdev,MAX_DEVICE,dev))<0) 314. gotoerror; 315. 316. /* compute major/minor number */ 317. devno=MKDEV(major,minor); 318. 319. /* allocate struct cdev */ 320. cdev=cdev_alloc(); 321. 322. /* initialise struct cdev */ 323. cdev_init(cdev,&pci_ops); 324. cdev->owner=THIS_MODULE; 325. 326. /* register cdev */ 327. ret=cdev_add(cdev,devno,1); 328. if(ret<0){ 329. dev_err(&(dev->dev),"Can't register character device\n"); 330. gotoerror; 331. } 332. pci_cdev[minor].cdev=cdev; 333. 334. dev_info(&(dev->dev),"%s The major device number is %d (%d).\n", 335. "Registeration is a success",MAJOR(devno),MINOR(devno)); 336. dev_info(&(dev->dev),"If you want to talk to the device driver,\n"); 337. dev_info(&(dev->dev),"you'll have to create a device file.\n"); 338. dev_info(&(dev->dev),"We suggest you use:\n"); 339. dev_info(&(dev->dev),"mknod %s c %d %d\n",DEVICE_NAME,MAJOR(devno),MINOR(devno)); 340. dev_info(&(dev->dev),"The device file name is important, because\n"); 341. dev_info(&(dev->dev),"the ioctl program assumes that's the\n"); 342. dev_info(&(dev->dev),"file you'll use.\n"); 343. 344. /* enable the device */ 345. pci_enable_device(dev); 346. 347. /* 'alloc' IO to talk with the card */ 348. if(pci_request_region(dev,BAR_PHOTO_MEM,"MEM-pci")==0){ 349. dev_err(&(dev->dev),"Can't request BAR2\n"); 350. cdev_del(cdev); 351. gotoerror; 352. } 353. 354. /* check that BAR_IO is *really* IO region */ 355. if((pci_resource_flags(dev,BAR_PHOTO_MEM)&IORESOURCE_MEM)!=IORESOURCE_MEM){ 356. dev_err(&(dev->dev),"BAR2 isn't an IO region\n"); 357. cdev_del(cdev); 358. gotoerror; 359. } 360. 361. 362. return1; 363. 364. error: 365. return0; 366. } 367. 368. /** 369. * This function is called when the driver is removed 370. * 371. */ 372. staticvoidpci_remove(structpci_dev*dev) 373. { 374. intminor; 375. structcdev*cdev; 376. 377. /* remove associated cdev */ 378. minor=pci_cdev_search_minor(pci_cdev,MAX_DEVICE,dev); 379. cdev=pci_cdev_search_cdev(pci_cdev,MAX_DEVICE,minor); 380. if(cdev!=NULL) 381. cdev_del(cdev); 382. 383. /* remove this device from pci_cdev */ 384. pci_cdev_del(pci_cdev,MAX_DEVICE,dev); 385. 386. /* release the IO region */ 387. pci_release_region(dev,BAR_IO); 388. } 389. 390. /** 391. * This structure holds informations about the pci driver 392. * 393. */ 394. staticstructpci_driver pci_driver={ 395. .name="pci", 396. .id_table=pci_ids, 397. .probe=pci_probe, 398. .remove=pci_remove, 399. }; 400. 401. 402. /** 403. * This function is called when the module is loaded 404. * 405. */ 406. staticint__init pci_init_module(void) 407. { 408. intret; 409. 410. printk(KERN_DEBUG"Module pci init\n"); 411. 412. /* allocate (several) major number */ 413. ret=alloc_chrdev_region(&devno,0,MAX_DEVICE,"buffer"); 414. if(ret<0){ 415. printk(KERN_ERR"Can't get major\n"); 416. returnret; 417. } 418. 419. /* get major number and save it in major */ 420. major=MAJOR(devno); 421. 422. /* initialise pci_cdev */ 423. pci_cdev_init(pci_cdev,MAX_DEVICE,MINOR(devno)); 424. 425. /* register pci driver */ 426. ret=pci_register_driver(&pci_driver); 427. if(ret<0){ 428. /* free major/minor number */ 429. unregister_chrdev_region(devno,1); 430. 431. printk(KERN_ERR"pci-driver: can't register pci driver\n"); 432. returnret; 433. } 434. 435. 436. return0; 437. } 438. 439. /** 440. * This function is called when the module is unloaded 441. * 442. */ 443. staticvoidpci_exit_module(void) 444. { 445. inti; 446. 447. /* unregister pci driver */ 448. pci_unregister_driver(&pci_driver); 449. 450. /* unregister character device */ 451. for(i=0;i