vme.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  1. /*
  2. * VME Bridge Framework
  3. *
  4. * Author: Martyn Welch <martyn.welch@ge.com>
  5. * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
  6. *
  7. * Based on work by Tom Armistead and Ajit Prem
  8. * Copyright 2004 Motorola Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/mm.h>
  18. #include <linux/types.h>
  19. #include <linux/kernel.h>
  20. #include <linux/errno.h>
  21. #include <linux/pci.h>
  22. #include <linux/poll.h>
  23. #include <linux/highmem.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/pagemap.h>
  26. #include <linux/device.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/syscalls.h>
  29. #include <linux/mutex.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/slab.h>
  32. #include "vme.h"
  33. #include "vme_bridge.h"
  34. /* Bitmask and mutex to keep track of bridge numbers */
  35. static unsigned int vme_bus_numbers;
  36. static DEFINE_MUTEX(vme_bus_num_mtx);
  37. static void __exit vme_exit(void);
  38. static int __init vme_init(void);
  39. /*
  40. * Find the bridge resource associated with a specific device resource
  41. */
  42. static struct vme_bridge *dev_to_bridge(struct device *dev)
  43. {
  44. return dev->platform_data;
  45. }
  46. /*
  47. * Find the bridge that the resource is associated with.
  48. */
  49. static struct vme_bridge *find_bridge(struct vme_resource *resource)
  50. {
  51. /* Get list to search */
  52. switch (resource->type) {
  53. case VME_MASTER:
  54. return list_entry(resource->entry, struct vme_master_resource,
  55. list)->parent;
  56. break;
  57. case VME_SLAVE:
  58. return list_entry(resource->entry, struct vme_slave_resource,
  59. list)->parent;
  60. break;
  61. case VME_DMA:
  62. return list_entry(resource->entry, struct vme_dma_resource,
  63. list)->parent;
  64. break;
  65. case VME_LM:
  66. return list_entry(resource->entry, struct vme_lm_resource,
  67. list)->parent;
  68. break;
  69. default:
  70. printk(KERN_ERR "Unknown resource type\n");
  71. return NULL;
  72. break;
  73. }
  74. }
  75. /*
  76. * Allocate a contiguous block of memory for use by the driver. This is used to
  77. * create the buffers for the slave windows.
  78. *
  79. * XXX VME bridges could be available on buses other than PCI. At the momment
  80. * this framework only supports PCI devices.
  81. */
  82. void *vme_alloc_consistent(struct vme_resource *resource, size_t size,
  83. dma_addr_t *dma)
  84. {
  85. struct vme_bridge *bridge;
  86. struct pci_dev *pdev;
  87. if (resource == NULL) {
  88. printk(KERN_ERR "No resource\n");
  89. return NULL;
  90. }
  91. bridge = find_bridge(resource);
  92. if (bridge == NULL) {
  93. printk(KERN_ERR "Can't find bridge\n");
  94. return NULL;
  95. }
  96. /* Find pci_dev container of dev */
  97. if (bridge->parent == NULL) {
  98. printk(KERN_ERR "Dev entry NULL\n");
  99. return NULL;
  100. }
  101. pdev = container_of(bridge->parent, struct pci_dev, dev);
  102. return pci_alloc_consistent(pdev, size, dma);
  103. }
  104. EXPORT_SYMBOL(vme_alloc_consistent);
  105. /*
  106. * Free previously allocated contiguous block of memory.
  107. *
  108. * XXX VME bridges could be available on buses other than PCI. At the momment
  109. * this framework only supports PCI devices.
  110. */
  111. void vme_free_consistent(struct vme_resource *resource, size_t size,
  112. void *vaddr, dma_addr_t dma)
  113. {
  114. struct vme_bridge *bridge;
  115. struct pci_dev *pdev;
  116. if (resource == NULL) {
  117. printk(KERN_ERR "No resource\n");
  118. return;
  119. }
  120. bridge = find_bridge(resource);
  121. if (bridge == NULL) {
  122. printk(KERN_ERR "Can't find bridge\n");
  123. return;
  124. }
  125. /* Find pci_dev container of dev */
  126. pdev = container_of(bridge->parent, struct pci_dev, dev);
  127. pci_free_consistent(pdev, size, vaddr, dma);
  128. }
  129. EXPORT_SYMBOL(vme_free_consistent);
  130. size_t vme_get_size(struct vme_resource *resource)
  131. {
  132. int enabled, retval;
  133. unsigned long long base, size;
  134. dma_addr_t buf_base;
  135. vme_address_t aspace;
  136. vme_cycle_t cycle;
  137. vme_width_t dwidth;
  138. switch (resource->type) {
  139. case VME_MASTER:
  140. retval = vme_master_get(resource, &enabled, &base, &size,
  141. &aspace, &cycle, &dwidth);
  142. return size;
  143. break;
  144. case VME_SLAVE:
  145. retval = vme_slave_get(resource, &enabled, &base, &size,
  146. &buf_base, &aspace, &cycle);
  147. return size;
  148. break;
  149. case VME_DMA:
  150. return 0;
  151. break;
  152. default:
  153. printk(KERN_ERR "Unknown resource type\n");
  154. return 0;
  155. break;
  156. }
  157. }
  158. EXPORT_SYMBOL(vme_get_size);
  159. static int vme_check_window(vme_address_t aspace, unsigned long long vme_base,
  160. unsigned long long size)
  161. {
  162. int retval = 0;
  163. switch (aspace) {
  164. case VME_A16:
  165. if (((vme_base + size) > VME_A16_MAX) ||
  166. (vme_base > VME_A16_MAX))
  167. retval = -EFAULT;
  168. break;
  169. case VME_A24:
  170. if (((vme_base + size) > VME_A24_MAX) ||
  171. (vme_base > VME_A24_MAX))
  172. retval = -EFAULT;
  173. break;
  174. case VME_A32:
  175. if (((vme_base + size) > VME_A32_MAX) ||
  176. (vme_base > VME_A32_MAX))
  177. retval = -EFAULT;
  178. break;
  179. case VME_A64:
  180. /*
  181. * Any value held in an unsigned long long can be used as the
  182. * base
  183. */
  184. break;
  185. case VME_CRCSR:
  186. if (((vme_base + size) > VME_CRCSR_MAX) ||
  187. (vme_base > VME_CRCSR_MAX))
  188. retval = -EFAULT;
  189. break;
  190. case VME_USER1:
  191. case VME_USER2:
  192. case VME_USER3:
  193. case VME_USER4:
  194. /* User Defined */
  195. break;
  196. default:
  197. printk(KERN_ERR "Invalid address space\n");
  198. retval = -EINVAL;
  199. break;
  200. }
  201. return retval;
  202. }
  203. /*
  204. * Request a slave image with specific attributes, return some unique
  205. * identifier.
  206. */
  207. struct vme_resource *vme_slave_request(struct device *dev,
  208. vme_address_t address, vme_cycle_t cycle)
  209. {
  210. struct vme_bridge *bridge;
  211. struct list_head *slave_pos = NULL;
  212. struct vme_slave_resource *allocated_image = NULL;
  213. struct vme_slave_resource *slave_image = NULL;
  214. struct vme_resource *resource = NULL;
  215. bridge = dev_to_bridge(dev);
  216. if (bridge == NULL) {
  217. printk(KERN_ERR "Can't find VME bus\n");
  218. goto err_bus;
  219. }
  220. /* Loop through slave resources */
  221. list_for_each(slave_pos, &bridge->slave_resources) {
  222. slave_image = list_entry(slave_pos,
  223. struct vme_slave_resource, list);
  224. if (slave_image == NULL) {
  225. printk(KERN_ERR "Registered NULL Slave resource\n");
  226. continue;
  227. }
  228. /* Find an unlocked and compatible image */
  229. mutex_lock(&slave_image->mtx);
  230. if (((slave_image->address_attr & address) == address) &&
  231. ((slave_image->cycle_attr & cycle) == cycle) &&
  232. (slave_image->locked == 0)) {
  233. slave_image->locked = 1;
  234. mutex_unlock(&slave_image->mtx);
  235. allocated_image = slave_image;
  236. break;
  237. }
  238. mutex_unlock(&slave_image->mtx);
  239. }
  240. /* No free image */
  241. if (allocated_image == NULL)
  242. goto err_image;
  243. resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
  244. if (resource == NULL) {
  245. printk(KERN_WARNING "Unable to allocate resource structure\n");
  246. goto err_alloc;
  247. }
  248. resource->type = VME_SLAVE;
  249. resource->entry = &allocated_image->list;
  250. return resource;
  251. err_alloc:
  252. /* Unlock image */
  253. mutex_lock(&slave_image->mtx);
  254. slave_image->locked = 0;
  255. mutex_unlock(&slave_image->mtx);
  256. err_image:
  257. err_bus:
  258. return NULL;
  259. }
  260. EXPORT_SYMBOL(vme_slave_request);
  261. int vme_slave_set(struct vme_resource *resource, int enabled,
  262. unsigned long long vme_base, unsigned long long size,
  263. dma_addr_t buf_base, vme_address_t aspace, vme_cycle_t cycle)
  264. {
  265. struct vme_bridge *bridge = find_bridge(resource);
  266. struct vme_slave_resource *image;
  267. int retval;
  268. if (resource->type != VME_SLAVE) {
  269. printk(KERN_ERR "Not a slave resource\n");
  270. return -EINVAL;
  271. }
  272. image = list_entry(resource->entry, struct vme_slave_resource, list);
  273. if (bridge->slave_set == NULL) {
  274. printk(KERN_ERR "Function not supported\n");
  275. return -ENOSYS;
  276. }
  277. if (!(((image->address_attr & aspace) == aspace) &&
  278. ((image->cycle_attr & cycle) == cycle))) {
  279. printk(KERN_ERR "Invalid attributes\n");
  280. return -EINVAL;
  281. }
  282. retval = vme_check_window(aspace, vme_base, size);
  283. if (retval)
  284. return retval;
  285. return bridge->slave_set(image, enabled, vme_base, size, buf_base,
  286. aspace, cycle);
  287. }
  288. EXPORT_SYMBOL(vme_slave_set);
  289. int vme_slave_get(struct vme_resource *resource, int *enabled,
  290. unsigned long long *vme_base, unsigned long long *size,
  291. dma_addr_t *buf_base, vme_address_t *aspace, vme_cycle_t *cycle)
  292. {
  293. struct vme_bridge *bridge = find_bridge(resource);
  294. struct vme_slave_resource *image;
  295. if (resource->type != VME_SLAVE) {
  296. printk(KERN_ERR "Not a slave resource\n");
  297. return -EINVAL;
  298. }
  299. image = list_entry(resource->entry, struct vme_slave_resource, list);
  300. if (bridge->slave_get == NULL) {
  301. printk(KERN_ERR "vme_slave_get not supported\n");
  302. return -EINVAL;
  303. }
  304. return bridge->slave_get(image, enabled, vme_base, size, buf_base,
  305. aspace, cycle);
  306. }
  307. EXPORT_SYMBOL(vme_slave_get);
  308. void vme_slave_free(struct vme_resource *resource)
  309. {
  310. struct vme_slave_resource *slave_image;
  311. if (resource->type != VME_SLAVE) {
  312. printk(KERN_ERR "Not a slave resource\n");
  313. return;
  314. }
  315. slave_image = list_entry(resource->entry, struct vme_slave_resource,
  316. list);
  317. if (slave_image == NULL) {
  318. printk(KERN_ERR "Can't find slave resource\n");
  319. return;
  320. }
  321. /* Unlock image */
  322. mutex_lock(&slave_image->mtx);
  323. if (slave_image->locked == 0)
  324. printk(KERN_ERR "Image is already free\n");
  325. slave_image->locked = 0;
  326. mutex_unlock(&slave_image->mtx);
  327. /* Free up resource memory */
  328. kfree(resource);
  329. }
  330. EXPORT_SYMBOL(vme_slave_free);
  331. /*
  332. * Request a master image with specific attributes, return some unique
  333. * identifier.
  334. */
  335. struct vme_resource *vme_master_request(struct device *dev,
  336. vme_address_t address, vme_cycle_t cycle, vme_width_t dwidth)
  337. {
  338. struct vme_bridge *bridge;
  339. struct list_head *master_pos = NULL;
  340. struct vme_master_resource *allocated_image = NULL;
  341. struct vme_master_resource *master_image = NULL;
  342. struct vme_resource *resource = NULL;
  343. bridge = dev_to_bridge(dev);
  344. if (bridge == NULL) {
  345. printk(KERN_ERR "Can't find VME bus\n");
  346. goto err_bus;
  347. }
  348. /* Loop through master resources */
  349. list_for_each(master_pos, &bridge->master_resources) {
  350. master_image = list_entry(master_pos,
  351. struct vme_master_resource, list);
  352. if (master_image == NULL) {
  353. printk(KERN_WARNING "Registered NULL master resource\n");
  354. continue;
  355. }
  356. /* Find an unlocked and compatible image */
  357. spin_lock(&master_image->lock);
  358. if (((master_image->address_attr & address) == address) &&
  359. ((master_image->cycle_attr & cycle) == cycle) &&
  360. ((master_image->width_attr & dwidth) == dwidth) &&
  361. (master_image->locked == 0)) {
  362. master_image->locked = 1;
  363. spin_unlock(&master_image->lock);
  364. allocated_image = master_image;
  365. break;
  366. }
  367. spin_unlock(&master_image->lock);
  368. }
  369. /* Check to see if we found a resource */
  370. if (allocated_image == NULL) {
  371. printk(KERN_ERR "Can't find a suitable resource\n");
  372. goto err_image;
  373. }
  374. resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
  375. if (resource == NULL) {
  376. printk(KERN_ERR "Unable to allocate resource structure\n");
  377. goto err_alloc;
  378. }
  379. resource->type = VME_MASTER;
  380. resource->entry = &allocated_image->list;
  381. return resource;
  382. err_alloc:
  383. /* Unlock image */
  384. spin_lock(&master_image->lock);
  385. master_image->locked = 0;
  386. spin_unlock(&master_image->lock);
  387. err_image:
  388. err_bus:
  389. return NULL;
  390. }
  391. EXPORT_SYMBOL(vme_master_request);
  392. int vme_master_set(struct vme_resource *resource, int enabled,
  393. unsigned long long vme_base, unsigned long long size,
  394. vme_address_t aspace, vme_cycle_t cycle, vme_width_t dwidth)
  395. {
  396. struct vme_bridge *bridge = find_bridge(resource);
  397. struct vme_master_resource *image;
  398. int retval;
  399. if (resource->type != VME_MASTER) {
  400. printk(KERN_ERR "Not a master resource\n");
  401. return -EINVAL;
  402. }
  403. image = list_entry(resource->entry, struct vme_master_resource, list);
  404. if (bridge->master_set == NULL) {
  405. printk(KERN_WARNING "vme_master_set not supported\n");
  406. return -EINVAL;
  407. }
  408. if (!(((image->address_attr & aspace) == aspace) &&
  409. ((image->cycle_attr & cycle) == cycle) &&
  410. ((image->width_attr & dwidth) == dwidth))) {
  411. printk(KERN_WARNING "Invalid attributes\n");
  412. return -EINVAL;
  413. }
  414. retval = vme_check_window(aspace, vme_base, size);
  415. if (retval)
  416. return retval;
  417. return bridge->master_set(image, enabled, vme_base, size, aspace,
  418. cycle, dwidth);
  419. }
  420. EXPORT_SYMBOL(vme_master_set);
  421. int vme_master_get(struct vme_resource *resource, int *enabled,
  422. unsigned long long *vme_base, unsigned long long *size,
  423. vme_address_t *aspace, vme_cycle_t *cycle, vme_width_t *dwidth)
  424. {
  425. struct vme_bridge *bridge = find_bridge(resource);
  426. struct vme_master_resource *image;
  427. if (resource->type != VME_MASTER) {
  428. printk(KERN_ERR "Not a master resource\n");
  429. return -EINVAL;
  430. }
  431. image = list_entry(resource->entry, struct vme_master_resource, list);
  432. if (bridge->master_get == NULL) {
  433. printk(KERN_WARNING "vme_master_set not supported\n");
  434. return -EINVAL;
  435. }
  436. return bridge->master_get(image, enabled, vme_base, size, aspace,
  437. cycle, dwidth);
  438. }
  439. EXPORT_SYMBOL(vme_master_get);
  440. /*
  441. * Read data out of VME space into a buffer.
  442. */
  443. ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,
  444. loff_t offset)
  445. {
  446. struct vme_bridge *bridge = find_bridge(resource);
  447. struct vme_master_resource *image;
  448. size_t length;
  449. if (bridge->master_read == NULL) {
  450. printk(KERN_WARNING "Reading from resource not supported\n");
  451. return -EINVAL;
  452. }
  453. if (resource->type != VME_MASTER) {
  454. printk(KERN_ERR "Not a master resource\n");
  455. return -EINVAL;
  456. }
  457. image = list_entry(resource->entry, struct vme_master_resource, list);
  458. length = vme_get_size(resource);
  459. if (offset > length) {
  460. printk(KERN_WARNING "Invalid Offset\n");
  461. return -EFAULT;
  462. }
  463. if ((offset + count) > length)
  464. count = length - offset;
  465. return bridge->master_read(image, buf, count, offset);
  466. }
  467. EXPORT_SYMBOL(vme_master_read);
  468. /*
  469. * Write data out to VME space from a buffer.
  470. */
  471. ssize_t vme_master_write(struct vme_resource *resource, void *buf,
  472. size_t count, loff_t offset)
  473. {
  474. struct vme_bridge *bridge = find_bridge(resource);
  475. struct vme_master_resource *image;
  476. size_t length;
  477. if (bridge->master_write == NULL) {
  478. printk(KERN_WARNING "Writing to resource not supported\n");
  479. return -EINVAL;
  480. }
  481. if (resource->type != VME_MASTER) {
  482. printk(KERN_ERR "Not a master resource\n");
  483. return -EINVAL;
  484. }
  485. image = list_entry(resource->entry, struct vme_master_resource, list);
  486. length = vme_get_size(resource);
  487. if (offset > length) {
  488. printk(KERN_WARNING "Invalid Offset\n");
  489. return -EFAULT;
  490. }
  491. if ((offset + count) > length)
  492. count = length - offset;
  493. return bridge->master_write(image, buf, count, offset);
  494. }
  495. EXPORT_SYMBOL(vme_master_write);
  496. /*
  497. * Perform RMW cycle to provided location.
  498. */
  499. unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,
  500. unsigned int compare, unsigned int swap, loff_t offset)
  501. {
  502. struct vme_bridge *bridge = find_bridge(resource);
  503. struct vme_master_resource *image;
  504. if (bridge->master_rmw == NULL) {
  505. printk(KERN_WARNING "Writing to resource not supported\n");
  506. return -EINVAL;
  507. }
  508. if (resource->type != VME_MASTER) {
  509. printk(KERN_ERR "Not a master resource\n");
  510. return -EINVAL;
  511. }
  512. image = list_entry(resource->entry, struct vme_master_resource, list);
  513. return bridge->master_rmw(image, mask, compare, swap, offset);
  514. }
  515. EXPORT_SYMBOL(vme_master_rmw);
  516. void vme_master_free(struct vme_resource *resource)
  517. {
  518. struct vme_master_resource *master_image;
  519. if (resource->type != VME_MASTER) {
  520. printk(KERN_ERR "Not a master resource\n");
  521. return;
  522. }
  523. master_image = list_entry(resource->entry, struct vme_master_resource,
  524. list);
  525. if (master_image == NULL) {
  526. printk(KERN_ERR "Can't find master resource\n");
  527. return;
  528. }
  529. /* Unlock image */
  530. spin_lock(&master_image->lock);
  531. if (master_image->locked == 0)
  532. printk(KERN_ERR "Image is already free\n");
  533. master_image->locked = 0;
  534. spin_unlock(&master_image->lock);
  535. /* Free up resource memory */
  536. kfree(resource);
  537. }
  538. EXPORT_SYMBOL(vme_master_free);
  539. /*
  540. * Request a DMA controller with specific attributes, return some unique
  541. * identifier.
  542. */
  543. struct vme_resource *vme_dma_request(struct device *dev, vme_dma_route_t route)
  544. {
  545. struct vme_bridge *bridge;
  546. struct list_head *dma_pos = NULL;
  547. struct vme_dma_resource *allocated_ctrlr = NULL;
  548. struct vme_dma_resource *dma_ctrlr = NULL;
  549. struct vme_resource *resource = NULL;
  550. /* XXX Not checking resource attributes */
  551. printk(KERN_ERR "No VME resource Attribute tests done\n");
  552. bridge = dev_to_bridge(dev);
  553. if (bridge == NULL) {
  554. printk(KERN_ERR "Can't find VME bus\n");
  555. goto err_bus;
  556. }
  557. /* Loop through DMA resources */
  558. list_for_each(dma_pos, &bridge->dma_resources) {
  559. dma_ctrlr = list_entry(dma_pos,
  560. struct vme_dma_resource, list);
  561. if (dma_ctrlr == NULL) {
  562. printk(KERN_ERR "Registered NULL DMA resource\n");
  563. continue;
  564. }
  565. /* Find an unlocked and compatible controller */
  566. mutex_lock(&dma_ctrlr->mtx);
  567. if (((dma_ctrlr->route_attr & route) == route) &&
  568. (dma_ctrlr->locked == 0)) {
  569. dma_ctrlr->locked = 1;
  570. mutex_unlock(&dma_ctrlr->mtx);
  571. allocated_ctrlr = dma_ctrlr;
  572. break;
  573. }
  574. mutex_unlock(&dma_ctrlr->mtx);
  575. }
  576. /* Check to see if we found a resource */
  577. if (allocated_ctrlr == NULL)
  578. goto err_ctrlr;
  579. resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
  580. if (resource == NULL) {
  581. printk(KERN_WARNING "Unable to allocate resource structure\n");
  582. goto err_alloc;
  583. }
  584. resource->type = VME_DMA;
  585. resource->entry = &allocated_ctrlr->list;
  586. return resource;
  587. err_alloc:
  588. /* Unlock image */
  589. mutex_lock(&dma_ctrlr->mtx);
  590. dma_ctrlr->locked = 0;
  591. mutex_unlock(&dma_ctrlr->mtx);
  592. err_ctrlr:
  593. err_bus:
  594. return NULL;
  595. }
  596. EXPORT_SYMBOL(vme_dma_request);
  597. /*
  598. * Start new list
  599. */
  600. struct vme_dma_list *vme_new_dma_list(struct vme_resource *resource)
  601. {
  602. struct vme_dma_resource *ctrlr;
  603. struct vme_dma_list *dma_list;
  604. if (resource->type != VME_DMA) {
  605. printk(KERN_ERR "Not a DMA resource\n");
  606. return NULL;
  607. }
  608. ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
  609. dma_list = kmalloc(sizeof(struct vme_dma_list), GFP_KERNEL);
  610. if (dma_list == NULL) {
  611. printk(KERN_ERR "Unable to allocate memory for new dma list\n");
  612. return NULL;
  613. }
  614. INIT_LIST_HEAD(&dma_list->entries);
  615. dma_list->parent = ctrlr;
  616. mutex_init(&dma_list->mtx);
  617. return dma_list;
  618. }
  619. EXPORT_SYMBOL(vme_new_dma_list);
  620. /*
  621. * Create "Pattern" type attributes
  622. */
  623. struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern,
  624. vme_pattern_t type)
  625. {
  626. struct vme_dma_attr *attributes;
  627. struct vme_dma_pattern *pattern_attr;
  628. attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
  629. if (attributes == NULL) {
  630. printk(KERN_ERR "Unable to allocate memory for attributes "
  631. "structure\n");
  632. goto err_attr;
  633. }
  634. pattern_attr = kmalloc(sizeof(struct vme_dma_pattern), GFP_KERNEL);
  635. if (pattern_attr == NULL) {
  636. printk(KERN_ERR "Unable to allocate memory for pattern "
  637. "attributes\n");
  638. goto err_pat;
  639. }
  640. attributes->type = VME_DMA_PATTERN;
  641. attributes->private = (void *)pattern_attr;
  642. pattern_attr->pattern = pattern;
  643. pattern_attr->type = type;
  644. return attributes;
  645. err_pat:
  646. kfree(attributes);
  647. err_attr:
  648. return NULL;
  649. }
  650. EXPORT_SYMBOL(vme_dma_pattern_attribute);
  651. /*
  652. * Create "PCI" type attributes
  653. */
  654. struct vme_dma_attr *vme_dma_pci_attribute(dma_addr_t address)
  655. {
  656. struct vme_dma_attr *attributes;
  657. struct vme_dma_pci *pci_attr;
  658. /* XXX Run some sanity checks here */
  659. attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
  660. if (attributes == NULL) {
  661. printk(KERN_ERR "Unable to allocate memory for attributes "
  662. "structure\n");
  663. goto err_attr;
  664. }
  665. pci_attr = kmalloc(sizeof(struct vme_dma_pci), GFP_KERNEL);
  666. if (pci_attr == NULL) {
  667. printk(KERN_ERR "Unable to allocate memory for pci "
  668. "attributes\n");
  669. goto err_pci;
  670. }
  671. attributes->type = VME_DMA_PCI;
  672. attributes->private = (void *)pci_attr;
  673. pci_attr->address = address;
  674. return attributes;
  675. err_pci:
  676. kfree(attributes);
  677. err_attr:
  678. return NULL;
  679. }
  680. EXPORT_SYMBOL(vme_dma_pci_attribute);
  681. /*
  682. * Create "VME" type attributes
  683. */
  684. struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address,
  685. vme_address_t aspace, vme_cycle_t cycle, vme_width_t dwidth)
  686. {
  687. struct vme_dma_attr *attributes;
  688. struct vme_dma_vme *vme_attr;
  689. attributes = kmalloc(
  690. sizeof(struct vme_dma_attr), GFP_KERNEL);
  691. if (attributes == NULL) {
  692. printk(KERN_ERR "Unable to allocate memory for attributes "
  693. "structure\n");
  694. goto err_attr;
  695. }
  696. vme_attr = kmalloc(sizeof(struct vme_dma_vme), GFP_KERNEL);
  697. if (vme_attr == NULL) {
  698. printk(KERN_ERR "Unable to allocate memory for vme "
  699. "attributes\n");
  700. goto err_vme;
  701. }
  702. attributes->type = VME_DMA_VME;
  703. attributes->private = (void *)vme_attr;
  704. vme_attr->address = address;
  705. vme_attr->aspace = aspace;
  706. vme_attr->cycle = cycle;
  707. vme_attr->dwidth = dwidth;
  708. return attributes;
  709. err_vme:
  710. kfree(attributes);
  711. err_attr:
  712. return NULL;
  713. }
  714. EXPORT_SYMBOL(vme_dma_vme_attribute);
  715. /*
  716. * Free attribute
  717. */
  718. void vme_dma_free_attribute(struct vme_dma_attr *attributes)
  719. {
  720. kfree(attributes->private);
  721. kfree(attributes);
  722. }
  723. EXPORT_SYMBOL(vme_dma_free_attribute);
  724. int vme_dma_list_add(struct vme_dma_list *list, struct vme_dma_attr *src,
  725. struct vme_dma_attr *dest, size_t count)
  726. {
  727. struct vme_bridge *bridge = list->parent->parent;
  728. int retval;
  729. if (bridge->dma_list_add == NULL) {
  730. printk(KERN_WARNING "Link List DMA generation not supported\n");
  731. return -EINVAL;
  732. }
  733. if (!mutex_trylock(&list->mtx)) {
  734. printk(KERN_ERR "Link List already submitted\n");
  735. return -EINVAL;
  736. }
  737. retval = bridge->dma_list_add(list, src, dest, count);
  738. mutex_unlock(&list->mtx);
  739. return retval;
  740. }
  741. EXPORT_SYMBOL(vme_dma_list_add);
  742. int vme_dma_list_exec(struct vme_dma_list *list)
  743. {
  744. struct vme_bridge *bridge = list->parent->parent;
  745. int retval;
  746. if (bridge->dma_list_exec == NULL) {
  747. printk(KERN_ERR "Link List DMA execution not supported\n");
  748. return -EINVAL;
  749. }
  750. mutex_lock(&list->mtx);
  751. retval = bridge->dma_list_exec(list);
  752. mutex_unlock(&list->mtx);
  753. return retval;
  754. }
  755. EXPORT_SYMBOL(vme_dma_list_exec);
  756. int vme_dma_list_free(struct vme_dma_list *list)
  757. {
  758. struct vme_bridge *bridge = list->parent->parent;
  759. int retval;
  760. if (bridge->dma_list_empty == NULL) {
  761. printk(KERN_WARNING "Emptying of Link Lists not supported\n");
  762. return -EINVAL;
  763. }
  764. if (!mutex_trylock(&list->mtx)) {
  765. printk(KERN_ERR "Link List in use\n");
  766. return -EINVAL;
  767. }
  768. /*
  769. * Empty out all of the entries from the dma list. We need to go to the
  770. * low level driver as dma entries are driver specific.
  771. */
  772. retval = bridge->dma_list_empty(list);
  773. if (retval) {
  774. printk(KERN_ERR "Unable to empty link-list entries\n");
  775. mutex_unlock(&list->mtx);
  776. return retval;
  777. }
  778. mutex_unlock(&list->mtx);
  779. kfree(list);
  780. return retval;
  781. }
  782. EXPORT_SYMBOL(vme_dma_list_free);
  783. int vme_dma_free(struct vme_resource *resource)
  784. {
  785. struct vme_dma_resource *ctrlr;
  786. if (resource->type != VME_DMA) {
  787. printk(KERN_ERR "Not a DMA resource\n");
  788. return -EINVAL;
  789. }
  790. ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
  791. if (!mutex_trylock(&ctrlr->mtx)) {
  792. printk(KERN_ERR "Resource busy, can't free\n");
  793. return -EBUSY;
  794. }
  795. if (!(list_empty(&ctrlr->pending) && list_empty(&ctrlr->running))) {
  796. printk(KERN_WARNING "Resource still processing transfers\n");
  797. mutex_unlock(&ctrlr->mtx);
  798. return -EBUSY;
  799. }
  800. ctrlr->locked = 0;
  801. mutex_unlock(&ctrlr->mtx);
  802. return 0;
  803. }
  804. EXPORT_SYMBOL(vme_dma_free);
  805. void vme_irq_handler(struct vme_bridge *bridge, int level, int statid)
  806. {
  807. void (*call)(int, int, void *);
  808. void *priv_data;
  809. call = bridge->irq[level - 1].callback[statid].func;
  810. priv_data = bridge->irq[level - 1].callback[statid].priv_data;
  811. if (call != NULL)
  812. call(level, statid, priv_data);
  813. else
  814. printk(KERN_WARNING "Spurilous VME interrupt, level:%x, "
  815. "vector:%x\n", level, statid);
  816. }
  817. EXPORT_SYMBOL(vme_irq_handler);
  818. int vme_irq_request(struct device *dev, int level, int statid,
  819. void (*callback)(int, int, void *),
  820. void *priv_data)
  821. {
  822. struct vme_bridge *bridge;
  823. bridge = dev_to_bridge(dev);
  824. if (bridge == NULL) {
  825. printk(KERN_ERR "Can't find VME bus\n");
  826. return -EINVAL;
  827. }
  828. if ((level < 1) || (level > 7)) {
  829. printk(KERN_ERR "Invalid interrupt level\n");
  830. return -EINVAL;
  831. }
  832. if (bridge->irq_set == NULL) {
  833. printk(KERN_ERR "Configuring interrupts not supported\n");
  834. return -EINVAL;
  835. }
  836. mutex_lock(&bridge->irq_mtx);
  837. if (bridge->irq[level - 1].callback[statid].func) {
  838. mutex_unlock(&bridge->irq_mtx);
  839. printk(KERN_WARNING "VME Interrupt already taken\n");
  840. return -EBUSY;
  841. }
  842. bridge->irq[level - 1].count++;
  843. bridge->irq[level - 1].callback[statid].priv_data = priv_data;
  844. bridge->irq[level - 1].callback[statid].func = callback;
  845. /* Enable IRQ level */
  846. bridge->irq_set(bridge, level, 1, 1);
  847. mutex_unlock(&bridge->irq_mtx);
  848. return 0;
  849. }
  850. EXPORT_SYMBOL(vme_irq_request);
  851. void vme_irq_free(struct device *dev, int level, int statid)
  852. {
  853. struct vme_bridge *bridge;
  854. bridge = dev_to_bridge(dev);
  855. if (bridge == NULL) {
  856. printk(KERN_ERR "Can't find VME bus\n");
  857. return;
  858. }
  859. if ((level < 1) || (level > 7)) {
  860. printk(KERN_ERR "Invalid interrupt level\n");
  861. return;
  862. }
  863. if (bridge->irq_set == NULL) {
  864. printk(KERN_ERR "Configuring interrupts not supported\n");
  865. return;
  866. }
  867. mutex_lock(&bridge->irq_mtx);
  868. bridge->irq[level - 1].count--;
  869. /* Disable IRQ level if no more interrupts attached at this level*/
  870. if (bridge->irq[level - 1].count == 0)
  871. bridge->irq_set(bridge, level, 0, 1);
  872. bridge->irq[level - 1].callback[statid].func = NULL;
  873. bridge->irq[level - 1].callback[statid].priv_data = NULL;
  874. mutex_unlock(&bridge->irq_mtx);
  875. }
  876. EXPORT_SYMBOL(vme_irq_free);
  877. int vme_irq_generate(struct device *dev, int level, int statid)
  878. {
  879. struct vme_bridge *bridge;
  880. bridge = dev_to_bridge(dev);
  881. if (bridge == NULL) {
  882. printk(KERN_ERR "Can't find VME bus\n");
  883. return -EINVAL;
  884. }
  885. if ((level < 1) || (level > 7)) {
  886. printk(KERN_WARNING "Invalid interrupt level\n");
  887. return -EINVAL;
  888. }
  889. if (bridge->irq_generate == NULL) {
  890. printk(KERN_WARNING "Interrupt generation not supported\n");
  891. return -EINVAL;
  892. }
  893. return bridge->irq_generate(bridge, level, statid);
  894. }
  895. EXPORT_SYMBOL(vme_irq_generate);
  896. /*
  897. * Request the location monitor, return resource or NULL
  898. */
  899. struct vme_resource *vme_lm_request(struct device *dev)
  900. {
  901. struct vme_bridge *bridge;
  902. struct list_head *lm_pos = NULL;
  903. struct vme_lm_resource *allocated_lm = NULL;
  904. struct vme_lm_resource *lm = NULL;
  905. struct vme_resource *resource = NULL;
  906. bridge = dev_to_bridge(dev);
  907. if (bridge == NULL) {
  908. printk(KERN_ERR "Can't find VME bus\n");
  909. goto err_bus;
  910. }
  911. /* Loop through DMA resources */
  912. list_for_each(lm_pos, &bridge->lm_resources) {
  913. lm = list_entry(lm_pos,
  914. struct vme_lm_resource, list);
  915. if (lm == NULL) {
  916. printk(KERN_ERR "Registered NULL Location Monitor "
  917. "resource\n");
  918. continue;
  919. }
  920. /* Find an unlocked controller */
  921. mutex_lock(&lm->mtx);
  922. if (lm->locked == 0) {
  923. lm->locked = 1;
  924. mutex_unlock(&lm->mtx);
  925. allocated_lm = lm;
  926. break;
  927. }
  928. mutex_unlock(&lm->mtx);
  929. }
  930. /* Check to see if we found a resource */
  931. if (allocated_lm == NULL)
  932. goto err_lm;
  933. resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
  934. if (resource == NULL) {
  935. printk(KERN_ERR "Unable to allocate resource structure\n");
  936. goto err_alloc;
  937. }
  938. resource->type = VME_LM;
  939. resource->entry = &allocated_lm->list;
  940. return resource;
  941. err_alloc:
  942. /* Unlock image */
  943. mutex_lock(&lm->mtx);
  944. lm->locked = 0;
  945. mutex_unlock(&lm->mtx);
  946. err_lm:
  947. err_bus:
  948. return NULL;
  949. }
  950. EXPORT_SYMBOL(vme_lm_request);
  951. int vme_lm_count(struct vme_resource *resource)
  952. {
  953. struct vme_lm_resource *lm;
  954. if (resource->type != VME_LM) {
  955. printk(KERN_ERR "Not a Location Monitor resource\n");
  956. return -EINVAL;
  957. }
  958. lm = list_entry(resource->entry, struct vme_lm_resource, list);
  959. return lm->monitors;
  960. }
  961. EXPORT_SYMBOL(vme_lm_count);
  962. int vme_lm_set(struct vme_resource *resource, unsigned long long lm_base,
  963. vme_address_t aspace, vme_cycle_t cycle)
  964. {
  965. struct vme_bridge *bridge = find_bridge(resource);
  966. struct vme_lm_resource *lm;
  967. if (resource->type != VME_LM) {
  968. printk(KERN_ERR "Not a Location Monitor resource\n");
  969. return -EINVAL;
  970. }
  971. lm = list_entry(resource->entry, struct vme_lm_resource, list);
  972. if (bridge->lm_set == NULL) {
  973. printk(KERN_ERR "vme_lm_set not supported\n");
  974. return -EINVAL;
  975. }
  976. return bridge->lm_set(lm, lm_base, aspace, cycle);
  977. }
  978. EXPORT_SYMBOL(vme_lm_set);
  979. int vme_lm_get(struct vme_resource *resource, unsigned long long *lm_base,
  980. vme_address_t *aspace, vme_cycle_t *cycle)
  981. {
  982. struct vme_bridge *bridge = find_bridge(resource);
  983. struct vme_lm_resource *lm;
  984. if (resource->type != VME_LM) {
  985. printk(KERN_ERR "Not a Location Monitor resource\n");
  986. return -EINVAL;
  987. }
  988. lm = list_entry(resource->entry, struct vme_lm_resource, list);
  989. if (bridge->lm_get == NULL) {
  990. printk(KERN_ERR "vme_lm_get not supported\n");
  991. return -EINVAL;
  992. }
  993. return bridge->lm_get(lm, lm_base, aspace, cycle);
  994. }
  995. EXPORT_SYMBOL(vme_lm_get);
  996. int vme_lm_attach(struct vme_resource *resource, int monitor,
  997. void (*callback)(int))
  998. {
  999. struct vme_bridge *bridge = find_bridge(resource);
  1000. struct vme_lm_resource *lm;
  1001. if (resource->type != VME_LM) {
  1002. printk(KERN_ERR "Not a Location Monitor resource\n");
  1003. return -EINVAL;
  1004. }
  1005. lm = list_entry(resource->entry, struct vme_lm_resource, list);
  1006. if (bridge->lm_attach == NULL) {
  1007. printk(KERN_ERR "vme_lm_attach not supported\n");
  1008. return -EINVAL;
  1009. }
  1010. return bridge->lm_attach(lm, monitor, callback);
  1011. }
  1012. EXPORT_SYMBOL(vme_lm_attach);
  1013. int vme_lm_detach(struct vme_resource *resource, int monitor)
  1014. {
  1015. struct vme_bridge *bridge = find_bridge(resource);
  1016. struct vme_lm_resource *lm;
  1017. if (resource->type != VME_LM) {
  1018. printk(KERN_ERR "Not a Location Monitor resource\n");
  1019. return -EINVAL;
  1020. }
  1021. lm = list_entry(resource->entry, struct vme_lm_resource, list);
  1022. if (bridge->lm_detach == NULL) {
  1023. printk(KERN_ERR "vme_lm_detach not supported\n");
  1024. return -EINVAL;
  1025. }
  1026. return bridge->lm_detach(lm, monitor);
  1027. }
  1028. EXPORT_SYMBOL(vme_lm_detach);
  1029. void vme_lm_free(struct vme_resource *resource)
  1030. {
  1031. struct vme_lm_resource *lm;
  1032. if (resource->type != VME_LM) {
  1033. printk(KERN_ERR "Not a Location Monitor resource\n");
  1034. return;
  1035. }
  1036. lm = list_entry(resource->entry, struct vme_lm_resource, list);
  1037. mutex_lock(&lm->mtx);
  1038. /* XXX
  1039. * Check to see that there aren't any callbacks still attached, if
  1040. * there are we should probably be detaching them!
  1041. */
  1042. lm->locked = 0;
  1043. mutex_unlock(&lm->mtx);
  1044. kfree(resource);
  1045. }
  1046. EXPORT_SYMBOL(vme_lm_free);
  1047. int vme_slot_get(struct device *bus)
  1048. {
  1049. struct vme_bridge *bridge;
  1050. bridge = dev_to_bridge(bus);
  1051. if (bridge == NULL) {
  1052. printk(KERN_ERR "Can't find VME bus\n");
  1053. return -EINVAL;
  1054. }
  1055. if (bridge->slot_get == NULL) {
  1056. printk(KERN_WARNING "vme_slot_get not supported\n");
  1057. return -EINVAL;
  1058. }
  1059. return bridge->slot_get(bridge);
  1060. }
  1061. EXPORT_SYMBOL(vme_slot_get);
  1062. /* - Bridge Registration --------------------------------------------------- */
  1063. static int vme_alloc_bus_num(void)
  1064. {
  1065. int i;
  1066. mutex_lock(&vme_bus_num_mtx);
  1067. for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) {
  1068. if (((vme_bus_numbers >> i) & 0x1) == 0) {
  1069. vme_bus_numbers |= (0x1 << i);
  1070. break;
  1071. }
  1072. }
  1073. mutex_unlock(&vme_bus_num_mtx);
  1074. return i;
  1075. }
  1076. static void vme_free_bus_num(int bus)
  1077. {
  1078. mutex_lock(&vme_bus_num_mtx);
  1079. vme_bus_numbers &= ~(0x1 << bus);
  1080. mutex_unlock(&vme_bus_num_mtx);
  1081. }
  1082. int vme_register_bridge(struct vme_bridge *bridge)
  1083. {
  1084. struct device *dev;
  1085. int retval;
  1086. int i;
  1087. bridge->num = vme_alloc_bus_num();
  1088. /* This creates 32 vme "slot" devices. This equates to a slot for each
  1089. * ID available in a system conforming to the ANSI/VITA 1-1994
  1090. * specification.
  1091. */
  1092. for (i = 0; i < VME_SLOTS_MAX; i++) {
  1093. dev = &bridge->dev[i];
  1094. memset(dev, 0, sizeof(struct device));
  1095. dev->parent = bridge->parent;
  1096. dev->bus = &vme_bus_type;
  1097. /*
  1098. * We save a pointer to the bridge in platform_data so that we
  1099. * can get to it later. We keep driver_data for use by the
  1100. * driver that binds against the slot
  1101. */
  1102. dev->platform_data = bridge;
  1103. dev_set_name(dev, "vme-%x.%x", bridge->num, i + 1);
  1104. retval = device_register(dev);
  1105. if (retval)
  1106. goto err_reg;
  1107. }
  1108. return retval;
  1109. err_reg:
  1110. while (--i >= 0) {
  1111. dev = &bridge->dev[i];
  1112. device_unregister(dev);
  1113. }
  1114. vme_free_bus_num(bridge->num);
  1115. return retval;
  1116. }
  1117. EXPORT_SYMBOL(vme_register_bridge);
  1118. void vme_unregister_bridge(struct vme_bridge *bridge)
  1119. {
  1120. int i;
  1121. struct device *dev;
  1122. for (i = 0; i < VME_SLOTS_MAX; i++) {
  1123. dev = &bridge->dev[i];
  1124. device_unregister(dev);
  1125. }
  1126. vme_free_bus_num(bridge->num);
  1127. }
  1128. EXPORT_SYMBOL(vme_unregister_bridge);
  1129. /* - Driver Registration --------------------------------------------------- */
  1130. int vme_register_driver(struct vme_driver *drv)
  1131. {
  1132. drv->driver.name = drv->name;
  1133. drv->driver.bus = &vme_bus_type;
  1134. return driver_register(&drv->driver);
  1135. }
  1136. EXPORT_SYMBOL(vme_register_driver);
  1137. void vme_unregister_driver(struct vme_driver *drv)
  1138. {
  1139. driver_unregister(&drv->driver);
  1140. }
  1141. EXPORT_SYMBOL(vme_unregister_driver);
  1142. /* - Bus Registration ------------------------------------------------------ */
  1143. static int vme_calc_slot(struct device *dev)
  1144. {
  1145. struct vme_bridge *bridge;
  1146. int num;
  1147. bridge = dev_to_bridge(dev);
  1148. /* Determine slot number */
  1149. num = 0;
  1150. while (num < VME_SLOTS_MAX) {
  1151. if (&bridge->dev[num] == dev)
  1152. break;
  1153. num++;
  1154. }
  1155. if (num == VME_SLOTS_MAX) {
  1156. dev_err(dev, "Failed to identify slot\n");
  1157. num = 0;
  1158. goto err_dev;
  1159. }
  1160. num++;
  1161. err_dev:
  1162. return num;
  1163. }
  1164. static struct vme_driver *dev_to_vme_driver(struct device *dev)
  1165. {
  1166. if (dev->driver == NULL)
  1167. printk(KERN_ERR "Bugger dev->driver is NULL\n");
  1168. return container_of(dev->driver, struct vme_driver, driver);
  1169. }
  1170. static int vme_bus_match(struct device *dev, struct device_driver *drv)
  1171. {
  1172. struct vme_bridge *bridge;
  1173. struct vme_driver *driver;
  1174. int i, num;
  1175. bridge = dev_to_bridge(dev);
  1176. driver = container_of(drv, struct vme_driver, driver);
  1177. num = vme_calc_slot(dev);
  1178. if (!num)
  1179. goto err_dev;
  1180. if (driver->bind_table == NULL) {
  1181. dev_err(dev, "Bind table NULL\n");
  1182. goto err_table;
  1183. }
  1184. i = 0;
  1185. while ((driver->bind_table[i].bus != 0) ||
  1186. (driver->bind_table[i].slot != 0)) {
  1187. if (bridge->num == driver->bind_table[i].bus) {
  1188. if (num == driver->bind_table[i].slot)
  1189. return 1;
  1190. if (driver->bind_table[i].slot == VME_SLOT_ALL)
  1191. return 1;
  1192. if ((driver->bind_table[i].slot == VME_SLOT_CURRENT) &&
  1193. (num == vme_slot_get(dev)))
  1194. return 1;
  1195. }
  1196. i++;
  1197. }
  1198. err_dev:
  1199. err_table:
  1200. return 0;
  1201. }
  1202. static int vme_bus_probe(struct device *dev)
  1203. {
  1204. struct vme_bridge *bridge;
  1205. struct vme_driver *driver;
  1206. int retval = -ENODEV;
  1207. driver = dev_to_vme_driver(dev);
  1208. bridge = dev_to_bridge(dev);
  1209. if (driver->probe != NULL)
  1210. retval = driver->probe(dev, bridge->num, vme_calc_slot(dev));
  1211. return retval;
  1212. }
  1213. static int vme_bus_remove(struct device *dev)
  1214. {
  1215. struct vme_bridge *bridge;
  1216. struct vme_driver *driver;
  1217. int retval = -ENODEV;
  1218. driver = dev_to_vme_driver(dev);
  1219. bridge = dev_to_bridge(dev);
  1220. if (driver->remove != NULL)
  1221. retval = driver->remove(dev, bridge->num, vme_calc_slot(dev));
  1222. return retval;
  1223. }
  1224. struct bus_type vme_bus_type = {
  1225. .name = "vme",
  1226. .match = vme_bus_match,
  1227. .probe = vme_bus_probe,
  1228. .remove = vme_bus_remove,
  1229. };
  1230. EXPORT_SYMBOL(vme_bus_type);
  1231. static int __init vme_init(void)
  1232. {
  1233. return bus_register(&vme_bus_type);
  1234. }
  1235. static void __exit vme_exit(void)
  1236. {
  1237. bus_unregister(&vme_bus_type);
  1238. }
  1239. MODULE_DESCRIPTION("VME bridge driver framework");
  1240. MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com");
  1241. MODULE_LICENSE("GPL");
  1242. module_init(vme_init);
  1243. module_exit(vme_exit);