vme.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032
  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/init.h>
  16. #include <linux/export.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 <linux/vme.h>
  33. #include "vme_bridge.h"
  34. /* Bitmask and list of registered buses both protected by common mutex */
  35. static unsigned int vme_bus_numbers;
  36. static LIST_HEAD(vme_bus_list);
  37. static DEFINE_MUTEX(vme_buses_lock);
  38. static int __init vme_init(void);
  39. static struct vme_dev *dev_to_vme_dev(struct device *dev)
  40. {
  41. return container_of(dev, struct vme_dev, dev);
  42. }
  43. /*
  44. * Find the bridge that the resource is associated with.
  45. */
  46. static struct vme_bridge *find_bridge(struct vme_resource *resource)
  47. {
  48. /* Get list to search */
  49. switch (resource->type) {
  50. case VME_MASTER:
  51. return list_entry(resource->entry, struct vme_master_resource,
  52. list)->parent;
  53. break;
  54. case VME_SLAVE:
  55. return list_entry(resource->entry, struct vme_slave_resource,
  56. list)->parent;
  57. break;
  58. case VME_DMA:
  59. return list_entry(resource->entry, struct vme_dma_resource,
  60. list)->parent;
  61. break;
  62. case VME_LM:
  63. return list_entry(resource->entry, struct vme_lm_resource,
  64. list)->parent;
  65. break;
  66. default:
  67. printk(KERN_ERR "Unknown resource type\n");
  68. return NULL;
  69. break;
  70. }
  71. }
  72. /**
  73. * vme_free_consistent - Allocate contiguous memory.
  74. * @resource: Pointer to VME resource.
  75. * @size: Size of allocation required.
  76. * @dma: Pointer to variable to store physical address of allocation.
  77. *
  78. * Allocate a contiguous block of memory for use by the driver. This is used to
  79. * create the buffers for the slave windows.
  80. *
  81. * Return: Virtual address of allocation on success, NULL on failure.
  82. */
  83. void *vme_alloc_consistent(struct vme_resource *resource, size_t size,
  84. dma_addr_t *dma)
  85. {
  86. struct vme_bridge *bridge;
  87. if (!resource) {
  88. printk(KERN_ERR "No resource\n");
  89. return NULL;
  90. }
  91. bridge = find_bridge(resource);
  92. if (!bridge) {
  93. printk(KERN_ERR "Can't find bridge\n");
  94. return NULL;
  95. }
  96. if (!bridge->parent) {
  97. printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
  98. return NULL;
  99. }
  100. if (!bridge->alloc_consistent) {
  101. printk(KERN_ERR "alloc_consistent not supported by bridge %s\n",
  102. bridge->name);
  103. return NULL;
  104. }
  105. return bridge->alloc_consistent(bridge->parent, size, dma);
  106. }
  107. EXPORT_SYMBOL(vme_alloc_consistent);
  108. /**
  109. * vme_free_consistent - Free previously allocated memory.
  110. * @resource: Pointer to VME resource.
  111. * @size: Size of allocation to free.
  112. * @vaddr: Virtual address of allocation.
  113. * @dma: Physical address of allocation.
  114. *
  115. * Free previously allocated block of contiguous memory.
  116. */
  117. void vme_free_consistent(struct vme_resource *resource, size_t size,
  118. void *vaddr, dma_addr_t dma)
  119. {
  120. struct vme_bridge *bridge;
  121. if (!resource) {
  122. printk(KERN_ERR "No resource\n");
  123. return;
  124. }
  125. bridge = find_bridge(resource);
  126. if (!bridge) {
  127. printk(KERN_ERR "Can't find bridge\n");
  128. return;
  129. }
  130. if (!bridge->parent) {
  131. printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
  132. return;
  133. }
  134. if (!bridge->free_consistent) {
  135. printk(KERN_ERR "free_consistent not supported by bridge %s\n",
  136. bridge->name);
  137. return;
  138. }
  139. bridge->free_consistent(bridge->parent, size, vaddr, dma);
  140. }
  141. EXPORT_SYMBOL(vme_free_consistent);
  142. /**
  143. * vme_get_size - Helper function returning size of a VME window
  144. * @resource: Pointer to VME slave or master resource.
  145. *
  146. * Determine the size of the VME window provided. This is a helper
  147. * function, wrappering the call to vme_master_get or vme_slave_get
  148. * depending on the type of window resource handed to it.
  149. *
  150. * Return: Size of the window on success, zero on failure.
  151. */
  152. size_t vme_get_size(struct vme_resource *resource)
  153. {
  154. int enabled, retval;
  155. unsigned long long base, size;
  156. dma_addr_t buf_base;
  157. u32 aspace, cycle, dwidth;
  158. switch (resource->type) {
  159. case VME_MASTER:
  160. retval = vme_master_get(resource, &enabled, &base, &size,
  161. &aspace, &cycle, &dwidth);
  162. if (retval)
  163. return 0;
  164. return size;
  165. break;
  166. case VME_SLAVE:
  167. retval = vme_slave_get(resource, &enabled, &base, &size,
  168. &buf_base, &aspace, &cycle);
  169. if (retval)
  170. return 0;
  171. return size;
  172. break;
  173. case VME_DMA:
  174. return 0;
  175. break;
  176. default:
  177. printk(KERN_ERR "Unknown resource type\n");
  178. return 0;
  179. break;
  180. }
  181. }
  182. EXPORT_SYMBOL(vme_get_size);
  183. int vme_check_window(u32 aspace, unsigned long long vme_base,
  184. unsigned long long size)
  185. {
  186. int retval = 0;
  187. if (vme_base + size < size)
  188. return -EINVAL;
  189. switch (aspace) {
  190. case VME_A16:
  191. if (vme_base + size > VME_A16_MAX)
  192. retval = -EFAULT;
  193. break;
  194. case VME_A24:
  195. if (vme_base + size > VME_A24_MAX)
  196. retval = -EFAULT;
  197. break;
  198. case VME_A32:
  199. if (vme_base + size > VME_A32_MAX)
  200. retval = -EFAULT;
  201. break;
  202. case VME_A64:
  203. /* The VME_A64_MAX limit is actually U64_MAX + 1 */
  204. break;
  205. case VME_CRCSR:
  206. if (vme_base + size > VME_CRCSR_MAX)
  207. retval = -EFAULT;
  208. break;
  209. case VME_USER1:
  210. case VME_USER2:
  211. case VME_USER3:
  212. case VME_USER4:
  213. /* User Defined */
  214. break;
  215. default:
  216. printk(KERN_ERR "Invalid address space\n");
  217. retval = -EINVAL;
  218. break;
  219. }
  220. return retval;
  221. }
  222. EXPORT_SYMBOL(vme_check_window);
  223. static u32 vme_get_aspace(int am)
  224. {
  225. switch (am) {
  226. case 0x29:
  227. case 0x2D:
  228. return VME_A16;
  229. case 0x38:
  230. case 0x39:
  231. case 0x3A:
  232. case 0x3B:
  233. case 0x3C:
  234. case 0x3D:
  235. case 0x3E:
  236. case 0x3F:
  237. return VME_A24;
  238. case 0x8:
  239. case 0x9:
  240. case 0xA:
  241. case 0xB:
  242. case 0xC:
  243. case 0xD:
  244. case 0xE:
  245. case 0xF:
  246. return VME_A32;
  247. case 0x0:
  248. case 0x1:
  249. case 0x3:
  250. return VME_A64;
  251. }
  252. return 0;
  253. }
  254. /**
  255. * vme_slave_request - Request a VME slave window resource.
  256. * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
  257. * @address: Required VME address space.
  258. * @cycle: Required VME data transfer cycle type.
  259. *
  260. * Request use of a VME window resource capable of being set for the requested
  261. * address space and data transfer cycle.
  262. *
  263. * Return: Pointer to VME resource on success, NULL on failure.
  264. */
  265. struct vme_resource *vme_slave_request(struct vme_dev *vdev, u32 address,
  266. u32 cycle)
  267. {
  268. struct vme_bridge *bridge;
  269. struct list_head *slave_pos = NULL;
  270. struct vme_slave_resource *allocated_image = NULL;
  271. struct vme_slave_resource *slave_image = NULL;
  272. struct vme_resource *resource = NULL;
  273. bridge = vdev->bridge;
  274. if (!bridge) {
  275. printk(KERN_ERR "Can't find VME bus\n");
  276. goto err_bus;
  277. }
  278. /* Loop through slave resources */
  279. list_for_each(slave_pos, &bridge->slave_resources) {
  280. slave_image = list_entry(slave_pos,
  281. struct vme_slave_resource, list);
  282. if (!slave_image) {
  283. printk(KERN_ERR "Registered NULL Slave resource\n");
  284. continue;
  285. }
  286. /* Find an unlocked and compatible image */
  287. mutex_lock(&slave_image->mtx);
  288. if (((slave_image->address_attr & address) == address) &&
  289. ((slave_image->cycle_attr & cycle) == cycle) &&
  290. (slave_image->locked == 0)) {
  291. slave_image->locked = 1;
  292. mutex_unlock(&slave_image->mtx);
  293. allocated_image = slave_image;
  294. break;
  295. }
  296. mutex_unlock(&slave_image->mtx);
  297. }
  298. /* No free image */
  299. if (!allocated_image)
  300. goto err_image;
  301. resource = kmalloc(sizeof(*resource), GFP_KERNEL);
  302. if (!resource)
  303. goto err_alloc;
  304. resource->type = VME_SLAVE;
  305. resource->entry = &allocated_image->list;
  306. return resource;
  307. err_alloc:
  308. /* Unlock image */
  309. mutex_lock(&slave_image->mtx);
  310. slave_image->locked = 0;
  311. mutex_unlock(&slave_image->mtx);
  312. err_image:
  313. err_bus:
  314. return NULL;
  315. }
  316. EXPORT_SYMBOL(vme_slave_request);
  317. /**
  318. * vme_slave_set - Set VME slave window configuration.
  319. * @resource: Pointer to VME slave resource.
  320. * @enabled: State to which the window should be configured.
  321. * @vme_base: Base address for the window.
  322. * @size: Size of the VME window.
  323. * @buf_base: Based address of buffer used to provide VME slave window storage.
  324. * @aspace: VME address space for the VME window.
  325. * @cycle: VME data transfer cycle type for the VME window.
  326. *
  327. * Set configuration for provided VME slave window.
  328. *
  329. * Return: Zero on success, -EINVAL if operation is not supported on this
  330. * device, if an invalid resource has been provided or invalid
  331. * attributes are provided. Hardware specific errors may also be
  332. * returned.
  333. */
  334. int vme_slave_set(struct vme_resource *resource, int enabled,
  335. unsigned long long vme_base, unsigned long long size,
  336. dma_addr_t buf_base, u32 aspace, u32 cycle)
  337. {
  338. struct vme_bridge *bridge = find_bridge(resource);
  339. struct vme_slave_resource *image;
  340. int retval;
  341. if (resource->type != VME_SLAVE) {
  342. printk(KERN_ERR "Not a slave resource\n");
  343. return -EINVAL;
  344. }
  345. image = list_entry(resource->entry, struct vme_slave_resource, list);
  346. if (!bridge->slave_set) {
  347. printk(KERN_ERR "Function not supported\n");
  348. return -ENOSYS;
  349. }
  350. if (!(((image->address_attr & aspace) == aspace) &&
  351. ((image->cycle_attr & cycle) == cycle))) {
  352. printk(KERN_ERR "Invalid attributes\n");
  353. return -EINVAL;
  354. }
  355. retval = vme_check_window(aspace, vme_base, size);
  356. if (retval)
  357. return retval;
  358. return bridge->slave_set(image, enabled, vme_base, size, buf_base,
  359. aspace, cycle);
  360. }
  361. EXPORT_SYMBOL(vme_slave_set);
  362. /**
  363. * vme_slave_get - Retrieve VME slave window configuration.
  364. * @resource: Pointer to VME slave resource.
  365. * @enabled: Pointer to variable for storing state.
  366. * @vme_base: Pointer to variable for storing window base address.
  367. * @size: Pointer to variable for storing window size.
  368. * @buf_base: Pointer to variable for storing slave buffer base address.
  369. * @aspace: Pointer to variable for storing VME address space.
  370. * @cycle: Pointer to variable for storing VME data transfer cycle type.
  371. *
  372. * Return configuration for provided VME slave window.
  373. *
  374. * Return: Zero on success, -EINVAL if operation is not supported on this
  375. * device or if an invalid resource has been provided.
  376. */
  377. int vme_slave_get(struct vme_resource *resource, int *enabled,
  378. unsigned long long *vme_base, unsigned long long *size,
  379. dma_addr_t *buf_base, u32 *aspace, u32 *cycle)
  380. {
  381. struct vme_bridge *bridge = find_bridge(resource);
  382. struct vme_slave_resource *image;
  383. if (resource->type != VME_SLAVE) {
  384. printk(KERN_ERR "Not a slave resource\n");
  385. return -EINVAL;
  386. }
  387. image = list_entry(resource->entry, struct vme_slave_resource, list);
  388. if (!bridge->slave_get) {
  389. printk(KERN_ERR "vme_slave_get not supported\n");
  390. return -EINVAL;
  391. }
  392. return bridge->slave_get(image, enabled, vme_base, size, buf_base,
  393. aspace, cycle);
  394. }
  395. EXPORT_SYMBOL(vme_slave_get);
  396. /**
  397. * vme_slave_free - Free VME slave window
  398. * @resource: Pointer to VME slave resource.
  399. *
  400. * Free the provided slave resource so that it may be reallocated.
  401. */
  402. void vme_slave_free(struct vme_resource *resource)
  403. {
  404. struct vme_slave_resource *slave_image;
  405. if (resource->type != VME_SLAVE) {
  406. printk(KERN_ERR "Not a slave resource\n");
  407. return;
  408. }
  409. slave_image = list_entry(resource->entry, struct vme_slave_resource,
  410. list);
  411. if (!slave_image) {
  412. printk(KERN_ERR "Can't find slave resource\n");
  413. return;
  414. }
  415. /* Unlock image */
  416. mutex_lock(&slave_image->mtx);
  417. if (slave_image->locked == 0)
  418. printk(KERN_ERR "Image is already free\n");
  419. slave_image->locked = 0;
  420. mutex_unlock(&slave_image->mtx);
  421. /* Free up resource memory */
  422. kfree(resource);
  423. }
  424. EXPORT_SYMBOL(vme_slave_free);
  425. /**
  426. * vme_master_request - Request a VME master window resource.
  427. * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
  428. * @address: Required VME address space.
  429. * @cycle: Required VME data transfer cycle type.
  430. * @dwidth: Required VME data transfer width.
  431. *
  432. * Request use of a VME window resource capable of being set for the requested
  433. * address space, data transfer cycle and width.
  434. *
  435. * Return: Pointer to VME resource on success, NULL on failure.
  436. */
  437. struct vme_resource *vme_master_request(struct vme_dev *vdev, u32 address,
  438. u32 cycle, u32 dwidth)
  439. {
  440. struct vme_bridge *bridge;
  441. struct list_head *master_pos = NULL;
  442. struct vme_master_resource *allocated_image = NULL;
  443. struct vme_master_resource *master_image = NULL;
  444. struct vme_resource *resource = NULL;
  445. bridge = vdev->bridge;
  446. if (!bridge) {
  447. printk(KERN_ERR "Can't find VME bus\n");
  448. goto err_bus;
  449. }
  450. /* Loop through master resources */
  451. list_for_each(master_pos, &bridge->master_resources) {
  452. master_image = list_entry(master_pos,
  453. struct vme_master_resource, list);
  454. if (!master_image) {
  455. printk(KERN_WARNING "Registered NULL master resource\n");
  456. continue;
  457. }
  458. /* Find an unlocked and compatible image */
  459. spin_lock(&master_image->lock);
  460. if (((master_image->address_attr & address) == address) &&
  461. ((master_image->cycle_attr & cycle) == cycle) &&
  462. ((master_image->width_attr & dwidth) == dwidth) &&
  463. (master_image->locked == 0)) {
  464. master_image->locked = 1;
  465. spin_unlock(&master_image->lock);
  466. allocated_image = master_image;
  467. break;
  468. }
  469. spin_unlock(&master_image->lock);
  470. }
  471. /* Check to see if we found a resource */
  472. if (!allocated_image) {
  473. printk(KERN_ERR "Can't find a suitable resource\n");
  474. goto err_image;
  475. }
  476. resource = kmalloc(sizeof(*resource), GFP_KERNEL);
  477. if (!resource)
  478. goto err_alloc;
  479. resource->type = VME_MASTER;
  480. resource->entry = &allocated_image->list;
  481. return resource;
  482. err_alloc:
  483. /* Unlock image */
  484. spin_lock(&master_image->lock);
  485. master_image->locked = 0;
  486. spin_unlock(&master_image->lock);
  487. err_image:
  488. err_bus:
  489. return NULL;
  490. }
  491. EXPORT_SYMBOL(vme_master_request);
  492. /**
  493. * vme_master_set - Set VME master window configuration.
  494. * @resource: Pointer to VME master resource.
  495. * @enabled: State to which the window should be configured.
  496. * @vme_base: Base address for the window.
  497. * @size: Size of the VME window.
  498. * @aspace: VME address space for the VME window.
  499. * @cycle: VME data transfer cycle type for the VME window.
  500. * @dwidth: VME data transfer width for the VME window.
  501. *
  502. * Set configuration for provided VME master window.
  503. *
  504. * Return: Zero on success, -EINVAL if operation is not supported on this
  505. * device, if an invalid resource has been provided or invalid
  506. * attributes are provided. Hardware specific errors may also be
  507. * returned.
  508. */
  509. int vme_master_set(struct vme_resource *resource, int enabled,
  510. unsigned long long vme_base, unsigned long long size, u32 aspace,
  511. u32 cycle, u32 dwidth)
  512. {
  513. struct vme_bridge *bridge = find_bridge(resource);
  514. struct vme_master_resource *image;
  515. int retval;
  516. if (resource->type != VME_MASTER) {
  517. printk(KERN_ERR "Not a master resource\n");
  518. return -EINVAL;
  519. }
  520. image = list_entry(resource->entry, struct vme_master_resource, list);
  521. if (!bridge->master_set) {
  522. printk(KERN_WARNING "vme_master_set not supported\n");
  523. return -EINVAL;
  524. }
  525. if (!(((image->address_attr & aspace) == aspace) &&
  526. ((image->cycle_attr & cycle) == cycle) &&
  527. ((image->width_attr & dwidth) == dwidth))) {
  528. printk(KERN_WARNING "Invalid attributes\n");
  529. return -EINVAL;
  530. }
  531. retval = vme_check_window(aspace, vme_base, size);
  532. if (retval)
  533. return retval;
  534. return bridge->master_set(image, enabled, vme_base, size, aspace,
  535. cycle, dwidth);
  536. }
  537. EXPORT_SYMBOL(vme_master_set);
  538. /**
  539. * vme_master_get - Retrieve VME master window configuration.
  540. * @resource: Pointer to VME master resource.
  541. * @enabled: Pointer to variable for storing state.
  542. * @vme_base: Pointer to variable for storing window base address.
  543. * @size: Pointer to variable for storing window size.
  544. * @aspace: Pointer to variable for storing VME address space.
  545. * @cycle: Pointer to variable for storing VME data transfer cycle type.
  546. * @dwidth: Pointer to variable for storing VME data transfer width.
  547. *
  548. * Return configuration for provided VME master window.
  549. *
  550. * Return: Zero on success, -EINVAL if operation is not supported on this
  551. * device or if an invalid resource has been provided.
  552. */
  553. int vme_master_get(struct vme_resource *resource, int *enabled,
  554. unsigned long long *vme_base, unsigned long long *size, u32 *aspace,
  555. u32 *cycle, u32 *dwidth)
  556. {
  557. struct vme_bridge *bridge = find_bridge(resource);
  558. struct vme_master_resource *image;
  559. if (resource->type != VME_MASTER) {
  560. printk(KERN_ERR "Not a master resource\n");
  561. return -EINVAL;
  562. }
  563. image = list_entry(resource->entry, struct vme_master_resource, list);
  564. if (!bridge->master_get) {
  565. printk(KERN_WARNING "%s not supported\n", __func__);
  566. return -EINVAL;
  567. }
  568. return bridge->master_get(image, enabled, vme_base, size, aspace,
  569. cycle, dwidth);
  570. }
  571. EXPORT_SYMBOL(vme_master_get);
  572. /**
  573. * vme_master_write - Read data from VME space into a buffer.
  574. * @resource: Pointer to VME master resource.
  575. * @buf: Pointer to buffer where data should be transferred.
  576. * @count: Number of bytes to transfer.
  577. * @offset: Offset into VME master window at which to start transfer.
  578. *
  579. * Perform read of count bytes of data from location on VME bus which maps into
  580. * the VME master window at offset to buf.
  581. *
  582. * Return: Number of bytes read, -EINVAL if resource is not a VME master
  583. * resource or read operation is not supported. -EFAULT returned if
  584. * invalid offset is provided. Hardware specific errors may also be
  585. * returned.
  586. */
  587. ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,
  588. loff_t offset)
  589. {
  590. struct vme_bridge *bridge = find_bridge(resource);
  591. struct vme_master_resource *image;
  592. size_t length;
  593. if (!bridge->master_read) {
  594. printk(KERN_WARNING "Reading from resource not supported\n");
  595. return -EINVAL;
  596. }
  597. if (resource->type != VME_MASTER) {
  598. printk(KERN_ERR "Not a master resource\n");
  599. return -EINVAL;
  600. }
  601. image = list_entry(resource->entry, struct vme_master_resource, list);
  602. length = vme_get_size(resource);
  603. if (offset > length) {
  604. printk(KERN_WARNING "Invalid Offset\n");
  605. return -EFAULT;
  606. }
  607. if ((offset + count) > length)
  608. count = length - offset;
  609. return bridge->master_read(image, buf, count, offset);
  610. }
  611. EXPORT_SYMBOL(vme_master_read);
  612. /**
  613. * vme_master_write - Write data out to VME space from a buffer.
  614. * @resource: Pointer to VME master resource.
  615. * @buf: Pointer to buffer holding data to transfer.
  616. * @count: Number of bytes to transfer.
  617. * @offset: Offset into VME master window at which to start transfer.
  618. *
  619. * Perform write of count bytes of data from buf to location on VME bus which
  620. * maps into the VME master window at offset.
  621. *
  622. * Return: Number of bytes written, -EINVAL if resource is not a VME master
  623. * resource or write operation is not supported. -EFAULT returned if
  624. * invalid offset is provided. Hardware specific errors may also be
  625. * returned.
  626. */
  627. ssize_t vme_master_write(struct vme_resource *resource, void *buf,
  628. size_t count, loff_t offset)
  629. {
  630. struct vme_bridge *bridge = find_bridge(resource);
  631. struct vme_master_resource *image;
  632. size_t length;
  633. if (!bridge->master_write) {
  634. printk(KERN_WARNING "Writing to resource not supported\n");
  635. return -EINVAL;
  636. }
  637. if (resource->type != VME_MASTER) {
  638. printk(KERN_ERR "Not a master resource\n");
  639. return -EINVAL;
  640. }
  641. image = list_entry(resource->entry, struct vme_master_resource, list);
  642. length = vme_get_size(resource);
  643. if (offset > length) {
  644. printk(KERN_WARNING "Invalid Offset\n");
  645. return -EFAULT;
  646. }
  647. if ((offset + count) > length)
  648. count = length - offset;
  649. return bridge->master_write(image, buf, count, offset);
  650. }
  651. EXPORT_SYMBOL(vme_master_write);
  652. /**
  653. * vme_master_rmw - Perform read-modify-write cycle.
  654. * @resource: Pointer to VME master resource.
  655. * @mask: Bits to be compared and swapped in operation.
  656. * @compare: Bits to be compared with data read from offset.
  657. * @swap: Bits to be swapped in data read from offset.
  658. * @offset: Offset into VME master window at which to perform operation.
  659. *
  660. * Perform read-modify-write cycle on provided location:
  661. * - Location on VME bus is read.
  662. * - Bits selected by mask are compared with compare.
  663. * - Where a selected bit matches that in compare and are selected in swap,
  664. * the bit is swapped.
  665. * - Result written back to location on VME bus.
  666. *
  667. * Return: Bytes written on success, -EINVAL if resource is not a VME master
  668. * resource or RMW operation is not supported. Hardware specific
  669. * errors may also be returned.
  670. */
  671. unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,
  672. unsigned int compare, unsigned int swap, loff_t offset)
  673. {
  674. struct vme_bridge *bridge = find_bridge(resource);
  675. struct vme_master_resource *image;
  676. if (!bridge->master_rmw) {
  677. printk(KERN_WARNING "Writing to resource not supported\n");
  678. return -EINVAL;
  679. }
  680. if (resource->type != VME_MASTER) {
  681. printk(KERN_ERR "Not a master resource\n");
  682. return -EINVAL;
  683. }
  684. image = list_entry(resource->entry, struct vme_master_resource, list);
  685. return bridge->master_rmw(image, mask, compare, swap, offset);
  686. }
  687. EXPORT_SYMBOL(vme_master_rmw);
  688. /**
  689. * vme_master_mmap - Mmap region of VME master window.
  690. * @resource: Pointer to VME master resource.
  691. * @vma: Pointer to definition of user mapping.
  692. *
  693. * Memory map a region of the VME master window into user space.
  694. *
  695. * Return: Zero on success, -EINVAL if resource is not a VME master
  696. * resource or -EFAULT if map exceeds window size. Other generic mmap
  697. * errors may also be returned.
  698. */
  699. int vme_master_mmap(struct vme_resource *resource, struct vm_area_struct *vma)
  700. {
  701. struct vme_master_resource *image;
  702. phys_addr_t phys_addr;
  703. unsigned long vma_size;
  704. if (resource->type != VME_MASTER) {
  705. pr_err("Not a master resource\n");
  706. return -EINVAL;
  707. }
  708. image = list_entry(resource->entry, struct vme_master_resource, list);
  709. phys_addr = image->bus_resource.start + (vma->vm_pgoff << PAGE_SHIFT);
  710. vma_size = vma->vm_end - vma->vm_start;
  711. if (phys_addr + vma_size > image->bus_resource.end + 1) {
  712. pr_err("Map size cannot exceed the window size\n");
  713. return -EFAULT;
  714. }
  715. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  716. return vm_iomap_memory(vma, phys_addr, vma->vm_end - vma->vm_start);
  717. }
  718. EXPORT_SYMBOL(vme_master_mmap);
  719. /**
  720. * vme_master_free - Free VME master window
  721. * @resource: Pointer to VME master resource.
  722. *
  723. * Free the provided master resource so that it may be reallocated.
  724. */
  725. void vme_master_free(struct vme_resource *resource)
  726. {
  727. struct vme_master_resource *master_image;
  728. if (resource->type != VME_MASTER) {
  729. printk(KERN_ERR "Not a master resource\n");
  730. return;
  731. }
  732. master_image = list_entry(resource->entry, struct vme_master_resource,
  733. list);
  734. if (!master_image) {
  735. printk(KERN_ERR "Can't find master resource\n");
  736. return;
  737. }
  738. /* Unlock image */
  739. spin_lock(&master_image->lock);
  740. if (master_image->locked == 0)
  741. printk(KERN_ERR "Image is already free\n");
  742. master_image->locked = 0;
  743. spin_unlock(&master_image->lock);
  744. /* Free up resource memory */
  745. kfree(resource);
  746. }
  747. EXPORT_SYMBOL(vme_master_free);
  748. /**
  749. * vme_dma_request - Request a DMA controller.
  750. * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
  751. * @route: Required src/destination combination.
  752. *
  753. * Request a VME DMA controller with capability to perform transfers bewteen
  754. * requested source/destination combination.
  755. *
  756. * Return: Pointer to VME DMA resource on success, NULL on failure.
  757. */
  758. struct vme_resource *vme_dma_request(struct vme_dev *vdev, u32 route)
  759. {
  760. struct vme_bridge *bridge;
  761. struct list_head *dma_pos = NULL;
  762. struct vme_dma_resource *allocated_ctrlr = NULL;
  763. struct vme_dma_resource *dma_ctrlr = NULL;
  764. struct vme_resource *resource = NULL;
  765. /* XXX Not checking resource attributes */
  766. printk(KERN_ERR "No VME resource Attribute tests done\n");
  767. bridge = vdev->bridge;
  768. if (!bridge) {
  769. printk(KERN_ERR "Can't find VME bus\n");
  770. goto err_bus;
  771. }
  772. /* Loop through DMA resources */
  773. list_for_each(dma_pos, &bridge->dma_resources) {
  774. dma_ctrlr = list_entry(dma_pos,
  775. struct vme_dma_resource, list);
  776. if (!dma_ctrlr) {
  777. printk(KERN_ERR "Registered NULL DMA resource\n");
  778. continue;
  779. }
  780. /* Find an unlocked and compatible controller */
  781. mutex_lock(&dma_ctrlr->mtx);
  782. if (((dma_ctrlr->route_attr & route) == route) &&
  783. (dma_ctrlr->locked == 0)) {
  784. dma_ctrlr->locked = 1;
  785. mutex_unlock(&dma_ctrlr->mtx);
  786. allocated_ctrlr = dma_ctrlr;
  787. break;
  788. }
  789. mutex_unlock(&dma_ctrlr->mtx);
  790. }
  791. /* Check to see if we found a resource */
  792. if (!allocated_ctrlr)
  793. goto err_ctrlr;
  794. resource = kmalloc(sizeof(*resource), GFP_KERNEL);
  795. if (!resource)
  796. goto err_alloc;
  797. resource->type = VME_DMA;
  798. resource->entry = &allocated_ctrlr->list;
  799. return resource;
  800. err_alloc:
  801. /* Unlock image */
  802. mutex_lock(&dma_ctrlr->mtx);
  803. dma_ctrlr->locked = 0;
  804. mutex_unlock(&dma_ctrlr->mtx);
  805. err_ctrlr:
  806. err_bus:
  807. return NULL;
  808. }
  809. EXPORT_SYMBOL(vme_dma_request);
  810. /**
  811. * vme_new_dma_list - Create new VME DMA list.
  812. * @resource: Pointer to VME DMA resource.
  813. *
  814. * Create a new VME DMA list. It is the responsibility of the user to free
  815. * the list once it is no longer required with vme_dma_list_free().
  816. *
  817. * Return: Pointer to new VME DMA list, NULL on allocation failure or invalid
  818. * VME DMA resource.
  819. */
  820. struct vme_dma_list *vme_new_dma_list(struct vme_resource *resource)
  821. {
  822. struct vme_dma_list *dma_list;
  823. if (resource->type != VME_DMA) {
  824. printk(KERN_ERR "Not a DMA resource\n");
  825. return NULL;
  826. }
  827. dma_list = kmalloc(sizeof(*dma_list), GFP_KERNEL);
  828. if (!dma_list)
  829. return NULL;
  830. INIT_LIST_HEAD(&dma_list->entries);
  831. dma_list->parent = list_entry(resource->entry,
  832. struct vme_dma_resource,
  833. list);
  834. mutex_init(&dma_list->mtx);
  835. return dma_list;
  836. }
  837. EXPORT_SYMBOL(vme_new_dma_list);
  838. /**
  839. * vme_dma_pattern_attribute - Create "Pattern" type VME DMA list attribute.
  840. * @pattern: Value to use used as pattern
  841. * @type: Type of pattern to be written.
  842. *
  843. * Create VME DMA list attribute for pattern generation. It is the
  844. * responsibility of the user to free used attributes using
  845. * vme_dma_free_attribute().
  846. *
  847. * Return: Pointer to VME DMA attribute, NULL on failure.
  848. */
  849. struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern, u32 type)
  850. {
  851. struct vme_dma_attr *attributes;
  852. struct vme_dma_pattern *pattern_attr;
  853. attributes = kmalloc(sizeof(*attributes), GFP_KERNEL);
  854. if (!attributes)
  855. goto err_attr;
  856. pattern_attr = kmalloc(sizeof(*pattern_attr), GFP_KERNEL);
  857. if (!pattern_attr)
  858. goto err_pat;
  859. attributes->type = VME_DMA_PATTERN;
  860. attributes->private = (void *)pattern_attr;
  861. pattern_attr->pattern = pattern;
  862. pattern_attr->type = type;
  863. return attributes;
  864. err_pat:
  865. kfree(attributes);
  866. err_attr:
  867. return NULL;
  868. }
  869. EXPORT_SYMBOL(vme_dma_pattern_attribute);
  870. /**
  871. * vme_dma_pci_attribute - Create "PCI" type VME DMA list attribute.
  872. * @address: PCI base address for DMA transfer.
  873. *
  874. * Create VME DMA list attribute pointing to a location on PCI for DMA
  875. * transfers. It is the responsibility of the user to free used attributes
  876. * using vme_dma_free_attribute().
  877. *
  878. * Return: Pointer to VME DMA attribute, NULL on failure.
  879. */
  880. struct vme_dma_attr *vme_dma_pci_attribute(dma_addr_t address)
  881. {
  882. struct vme_dma_attr *attributes;
  883. struct vme_dma_pci *pci_attr;
  884. /* XXX Run some sanity checks here */
  885. attributes = kmalloc(sizeof(*attributes), GFP_KERNEL);
  886. if (!attributes)
  887. goto err_attr;
  888. pci_attr = kmalloc(sizeof(*pci_attr), GFP_KERNEL);
  889. if (!pci_attr)
  890. goto err_pci;
  891. attributes->type = VME_DMA_PCI;
  892. attributes->private = (void *)pci_attr;
  893. pci_attr->address = address;
  894. return attributes;
  895. err_pci:
  896. kfree(attributes);
  897. err_attr:
  898. return NULL;
  899. }
  900. EXPORT_SYMBOL(vme_dma_pci_attribute);
  901. /**
  902. * vme_dma_vme_attribute - Create "VME" type VME DMA list attribute.
  903. * @address: VME base address for DMA transfer.
  904. * @aspace: VME address space to use for DMA transfer.
  905. * @cycle: VME bus cycle to use for DMA transfer.
  906. * @dwidth: VME data width to use for DMA transfer.
  907. *
  908. * Create VME DMA list attribute pointing to a location on the VME bus for DMA
  909. * transfers. It is the responsibility of the user to free used attributes
  910. * using vme_dma_free_attribute().
  911. *
  912. * Return: Pointer to VME DMA attribute, NULL on failure.
  913. */
  914. struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address,
  915. u32 aspace, u32 cycle, u32 dwidth)
  916. {
  917. struct vme_dma_attr *attributes;
  918. struct vme_dma_vme *vme_attr;
  919. attributes = kmalloc(sizeof(*attributes), GFP_KERNEL);
  920. if (!attributes)
  921. goto err_attr;
  922. vme_attr = kmalloc(sizeof(*vme_attr), GFP_KERNEL);
  923. if (!vme_attr)
  924. goto err_vme;
  925. attributes->type = VME_DMA_VME;
  926. attributes->private = (void *)vme_attr;
  927. vme_attr->address = address;
  928. vme_attr->aspace = aspace;
  929. vme_attr->cycle = cycle;
  930. vme_attr->dwidth = dwidth;
  931. return attributes;
  932. err_vme:
  933. kfree(attributes);
  934. err_attr:
  935. return NULL;
  936. }
  937. EXPORT_SYMBOL(vme_dma_vme_attribute);
  938. /**
  939. * vme_dma_free_attribute - Free DMA list attribute.
  940. * @attributes: Pointer to DMA list attribute.
  941. *
  942. * Free VME DMA list attribute. VME DMA list attributes can be safely freed
  943. * once vme_dma_list_add() has returned.
  944. */
  945. void vme_dma_free_attribute(struct vme_dma_attr *attributes)
  946. {
  947. kfree(attributes->private);
  948. kfree(attributes);
  949. }
  950. EXPORT_SYMBOL(vme_dma_free_attribute);
  951. /**
  952. * vme_dma_list_add - Add enty to a VME DMA list.
  953. * @list: Pointer to VME list.
  954. * @src: Pointer to DMA list attribute to use as source.
  955. * @dest: Pointer to DMA list attribute to use as destination.
  956. * @count: Number of bytes to transfer.
  957. *
  958. * Add an entry to the provided VME DMA list. Entry requires pointers to source
  959. * and destination DMA attributes and a count.
  960. *
  961. * Please note, the attributes supported as source and destinations for
  962. * transfers are hardware dependent.
  963. *
  964. * Return: Zero on success, -EINVAL if operation is not supported on this
  965. * device or if the link list has already been submitted for execution.
  966. * Hardware specific errors also possible.
  967. */
  968. int vme_dma_list_add(struct vme_dma_list *list, struct vme_dma_attr *src,
  969. struct vme_dma_attr *dest, size_t count)
  970. {
  971. struct vme_bridge *bridge = list->parent->parent;
  972. int retval;
  973. if (!bridge->dma_list_add) {
  974. printk(KERN_WARNING "Link List DMA generation not supported\n");
  975. return -EINVAL;
  976. }
  977. if (!mutex_trylock(&list->mtx)) {
  978. printk(KERN_ERR "Link List already submitted\n");
  979. return -EINVAL;
  980. }
  981. retval = bridge->dma_list_add(list, src, dest, count);
  982. mutex_unlock(&list->mtx);
  983. return retval;
  984. }
  985. EXPORT_SYMBOL(vme_dma_list_add);
  986. /**
  987. * vme_dma_list_exec - Queue a VME DMA list for execution.
  988. * @list: Pointer to VME list.
  989. *
  990. * Queue the provided VME DMA list for execution. The call will return once the
  991. * list has been executed.
  992. *
  993. * Return: Zero on success, -EINVAL if operation is not supported on this
  994. * device. Hardware specific errors also possible.
  995. */
  996. int vme_dma_list_exec(struct vme_dma_list *list)
  997. {
  998. struct vme_bridge *bridge = list->parent->parent;
  999. int retval;
  1000. if (!bridge->dma_list_exec) {
  1001. printk(KERN_ERR "Link List DMA execution not supported\n");
  1002. return -EINVAL;
  1003. }
  1004. mutex_lock(&list->mtx);
  1005. retval = bridge->dma_list_exec(list);
  1006. mutex_unlock(&list->mtx);
  1007. return retval;
  1008. }
  1009. EXPORT_SYMBOL(vme_dma_list_exec);
  1010. /**
  1011. * vme_dma_list_free - Free a VME DMA list.
  1012. * @list: Pointer to VME list.
  1013. *
  1014. * Free the provided DMA list and all its entries.
  1015. *
  1016. * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource
  1017. * is still in use. Hardware specific errors also possible.
  1018. */
  1019. int vme_dma_list_free(struct vme_dma_list *list)
  1020. {
  1021. struct vme_bridge *bridge = list->parent->parent;
  1022. int retval;
  1023. if (!bridge->dma_list_empty) {
  1024. printk(KERN_WARNING "Emptying of Link Lists not supported\n");
  1025. return -EINVAL;
  1026. }
  1027. if (!mutex_trylock(&list->mtx)) {
  1028. printk(KERN_ERR "Link List in use\n");
  1029. return -EBUSY;
  1030. }
  1031. /*
  1032. * Empty out all of the entries from the DMA list. We need to go to the
  1033. * low level driver as DMA entries are driver specific.
  1034. */
  1035. retval = bridge->dma_list_empty(list);
  1036. if (retval) {
  1037. printk(KERN_ERR "Unable to empty link-list entries\n");
  1038. mutex_unlock(&list->mtx);
  1039. return retval;
  1040. }
  1041. mutex_unlock(&list->mtx);
  1042. kfree(list);
  1043. return retval;
  1044. }
  1045. EXPORT_SYMBOL(vme_dma_list_free);
  1046. /**
  1047. * vme_dma_free - Free a VME DMA resource.
  1048. * @resource: Pointer to VME DMA resource.
  1049. *
  1050. * Free the provided DMA resource so that it may be reallocated.
  1051. *
  1052. * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource
  1053. * is still active.
  1054. */
  1055. int vme_dma_free(struct vme_resource *resource)
  1056. {
  1057. struct vme_dma_resource *ctrlr;
  1058. if (resource->type != VME_DMA) {
  1059. printk(KERN_ERR "Not a DMA resource\n");
  1060. return -EINVAL;
  1061. }
  1062. ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
  1063. if (!mutex_trylock(&ctrlr->mtx)) {
  1064. printk(KERN_ERR "Resource busy, can't free\n");
  1065. return -EBUSY;
  1066. }
  1067. if (!(list_empty(&ctrlr->pending) && list_empty(&ctrlr->running))) {
  1068. printk(KERN_WARNING "Resource still processing transfers\n");
  1069. mutex_unlock(&ctrlr->mtx);
  1070. return -EBUSY;
  1071. }
  1072. ctrlr->locked = 0;
  1073. mutex_unlock(&ctrlr->mtx);
  1074. kfree(resource);
  1075. return 0;
  1076. }
  1077. EXPORT_SYMBOL(vme_dma_free);
  1078. void vme_bus_error_handler(struct vme_bridge *bridge,
  1079. unsigned long long address, int am)
  1080. {
  1081. struct list_head *handler_pos = NULL;
  1082. struct vme_error_handler *handler;
  1083. int handler_triggered = 0;
  1084. u32 aspace = vme_get_aspace(am);
  1085. list_for_each(handler_pos, &bridge->vme_error_handlers) {
  1086. handler = list_entry(handler_pos, struct vme_error_handler,
  1087. list);
  1088. if ((aspace == handler->aspace) &&
  1089. (address >= handler->start) &&
  1090. (address < handler->end)) {
  1091. if (!handler->num_errors)
  1092. handler->first_error = address;
  1093. if (handler->num_errors != UINT_MAX)
  1094. handler->num_errors++;
  1095. handler_triggered = 1;
  1096. }
  1097. }
  1098. if (!handler_triggered)
  1099. dev_err(bridge->parent,
  1100. "Unhandled VME access error at address 0x%llx\n",
  1101. address);
  1102. }
  1103. EXPORT_SYMBOL(vme_bus_error_handler);
  1104. struct vme_error_handler *vme_register_error_handler(
  1105. struct vme_bridge *bridge, u32 aspace,
  1106. unsigned long long address, size_t len)
  1107. {
  1108. struct vme_error_handler *handler;
  1109. handler = kmalloc(sizeof(*handler), GFP_ATOMIC);
  1110. if (!handler)
  1111. return NULL;
  1112. handler->aspace = aspace;
  1113. handler->start = address;
  1114. handler->end = address + len;
  1115. handler->num_errors = 0;
  1116. handler->first_error = 0;
  1117. list_add_tail(&handler->list, &bridge->vme_error_handlers);
  1118. return handler;
  1119. }
  1120. EXPORT_SYMBOL(vme_register_error_handler);
  1121. void vme_unregister_error_handler(struct vme_error_handler *handler)
  1122. {
  1123. list_del(&handler->list);
  1124. kfree(handler);
  1125. }
  1126. EXPORT_SYMBOL(vme_unregister_error_handler);
  1127. void vme_irq_handler(struct vme_bridge *bridge, int level, int statid)
  1128. {
  1129. void (*call)(int, int, void *);
  1130. void *priv_data;
  1131. call = bridge->irq[level - 1].callback[statid].func;
  1132. priv_data = bridge->irq[level - 1].callback[statid].priv_data;
  1133. if (call)
  1134. call(level, statid, priv_data);
  1135. else
  1136. printk(KERN_WARNING "Spurious VME interrupt, level:%x, vector:%x\n",
  1137. level, statid);
  1138. }
  1139. EXPORT_SYMBOL(vme_irq_handler);
  1140. /**
  1141. * vme_irq_request - Request a specific VME interrupt.
  1142. * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
  1143. * @level: Interrupt priority being requested.
  1144. * @statid: Interrupt vector being requested.
  1145. * @callback: Pointer to callback function called when VME interrupt/vector
  1146. * received.
  1147. * @priv_data: Generic pointer that will be passed to the callback function.
  1148. *
  1149. * Request callback to be attached as a handler for VME interrupts with provided
  1150. * level and statid.
  1151. *
  1152. * Return: Zero on success, -EINVAL on invalid vme device, level or if the
  1153. * function is not supported, -EBUSY if the level/statid combination is
  1154. * already in use. Hardware specific errors also possible.
  1155. */
  1156. int vme_irq_request(struct vme_dev *vdev, int level, int statid,
  1157. void (*callback)(int, int, void *),
  1158. void *priv_data)
  1159. {
  1160. struct vme_bridge *bridge;
  1161. bridge = vdev->bridge;
  1162. if (!bridge) {
  1163. printk(KERN_ERR "Can't find VME bus\n");
  1164. return -EINVAL;
  1165. }
  1166. if ((level < 1) || (level > 7)) {
  1167. printk(KERN_ERR "Invalid interrupt level\n");
  1168. return -EINVAL;
  1169. }
  1170. if (!bridge->irq_set) {
  1171. printk(KERN_ERR "Configuring interrupts not supported\n");
  1172. return -EINVAL;
  1173. }
  1174. mutex_lock(&bridge->irq_mtx);
  1175. if (bridge->irq[level - 1].callback[statid].func) {
  1176. mutex_unlock(&bridge->irq_mtx);
  1177. printk(KERN_WARNING "VME Interrupt already taken\n");
  1178. return -EBUSY;
  1179. }
  1180. bridge->irq[level - 1].count++;
  1181. bridge->irq[level - 1].callback[statid].priv_data = priv_data;
  1182. bridge->irq[level - 1].callback[statid].func = callback;
  1183. /* Enable IRQ level */
  1184. bridge->irq_set(bridge, level, 1, 1);
  1185. mutex_unlock(&bridge->irq_mtx);
  1186. return 0;
  1187. }
  1188. EXPORT_SYMBOL(vme_irq_request);
  1189. /**
  1190. * vme_irq_free - Free a VME interrupt.
  1191. * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
  1192. * @level: Interrupt priority of interrupt being freed.
  1193. * @statid: Interrupt vector of interrupt being freed.
  1194. *
  1195. * Remove previously attached callback from VME interrupt priority/vector.
  1196. */
  1197. void vme_irq_free(struct vme_dev *vdev, int level, int statid)
  1198. {
  1199. struct vme_bridge *bridge;
  1200. bridge = vdev->bridge;
  1201. if (!bridge) {
  1202. printk(KERN_ERR "Can't find VME bus\n");
  1203. return;
  1204. }
  1205. if ((level < 1) || (level > 7)) {
  1206. printk(KERN_ERR "Invalid interrupt level\n");
  1207. return;
  1208. }
  1209. if (!bridge->irq_set) {
  1210. printk(KERN_ERR "Configuring interrupts not supported\n");
  1211. return;
  1212. }
  1213. mutex_lock(&bridge->irq_mtx);
  1214. bridge->irq[level - 1].count--;
  1215. /* Disable IRQ level if no more interrupts attached at this level*/
  1216. if (bridge->irq[level - 1].count == 0)
  1217. bridge->irq_set(bridge, level, 0, 1);
  1218. bridge->irq[level - 1].callback[statid].func = NULL;
  1219. bridge->irq[level - 1].callback[statid].priv_data = NULL;
  1220. mutex_unlock(&bridge->irq_mtx);
  1221. }
  1222. EXPORT_SYMBOL(vme_irq_free);
  1223. /**
  1224. * vme_irq_generate - Generate VME interrupt.
  1225. * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
  1226. * @level: Interrupt priority at which to assert the interrupt.
  1227. * @statid: Interrupt vector to associate with the interrupt.
  1228. *
  1229. * Generate a VME interrupt of the provided level and with the provided
  1230. * statid.
  1231. *
  1232. * Return: Zero on success, -EINVAL on invalid vme device, level or if the
  1233. * function is not supported. Hardware specific errors also possible.
  1234. */
  1235. int vme_irq_generate(struct vme_dev *vdev, int level, int statid)
  1236. {
  1237. struct vme_bridge *bridge;
  1238. bridge = vdev->bridge;
  1239. if (!bridge) {
  1240. printk(KERN_ERR "Can't find VME bus\n");
  1241. return -EINVAL;
  1242. }
  1243. if ((level < 1) || (level > 7)) {
  1244. printk(KERN_WARNING "Invalid interrupt level\n");
  1245. return -EINVAL;
  1246. }
  1247. if (!bridge->irq_generate) {
  1248. printk(KERN_WARNING "Interrupt generation not supported\n");
  1249. return -EINVAL;
  1250. }
  1251. return bridge->irq_generate(bridge, level, statid);
  1252. }
  1253. EXPORT_SYMBOL(vme_irq_generate);
  1254. /**
  1255. * vme_lm_request - Request a VME location monitor
  1256. * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
  1257. *
  1258. * Allocate a location monitor resource to the driver. A location monitor
  1259. * allows the driver to monitor accesses to a contiguous number of
  1260. * addresses on the VME bus.
  1261. *
  1262. * Return: Pointer to a VME resource on success or NULL on failure.
  1263. */
  1264. struct vme_resource *vme_lm_request(struct vme_dev *vdev)
  1265. {
  1266. struct vme_bridge *bridge;
  1267. struct list_head *lm_pos = NULL;
  1268. struct vme_lm_resource *allocated_lm = NULL;
  1269. struct vme_lm_resource *lm = NULL;
  1270. struct vme_resource *resource = NULL;
  1271. bridge = vdev->bridge;
  1272. if (!bridge) {
  1273. printk(KERN_ERR "Can't find VME bus\n");
  1274. goto err_bus;
  1275. }
  1276. /* Loop through LM resources */
  1277. list_for_each(lm_pos, &bridge->lm_resources) {
  1278. lm = list_entry(lm_pos,
  1279. struct vme_lm_resource, list);
  1280. if (!lm) {
  1281. printk(KERN_ERR "Registered NULL Location Monitor resource\n");
  1282. continue;
  1283. }
  1284. /* Find an unlocked controller */
  1285. mutex_lock(&lm->mtx);
  1286. if (lm->locked == 0) {
  1287. lm->locked = 1;
  1288. mutex_unlock(&lm->mtx);
  1289. allocated_lm = lm;
  1290. break;
  1291. }
  1292. mutex_unlock(&lm->mtx);
  1293. }
  1294. /* Check to see if we found a resource */
  1295. if (!allocated_lm)
  1296. goto err_lm;
  1297. resource = kmalloc(sizeof(*resource), GFP_KERNEL);
  1298. if (!resource)
  1299. goto err_alloc;
  1300. resource->type = VME_LM;
  1301. resource->entry = &allocated_lm->list;
  1302. return resource;
  1303. err_alloc:
  1304. /* Unlock image */
  1305. mutex_lock(&lm->mtx);
  1306. lm->locked = 0;
  1307. mutex_unlock(&lm->mtx);
  1308. err_lm:
  1309. err_bus:
  1310. return NULL;
  1311. }
  1312. EXPORT_SYMBOL(vme_lm_request);
  1313. /**
  1314. * vme_lm_count - Determine number of VME Addresses monitored
  1315. * @resource: Pointer to VME location monitor resource.
  1316. *
  1317. * The number of contiguous addresses monitored is hardware dependent.
  1318. * Return the number of contiguous addresses monitored by the
  1319. * location monitor.
  1320. *
  1321. * Return: Count of addresses monitored or -EINVAL when provided with an
  1322. * invalid location monitor resource.
  1323. */
  1324. int vme_lm_count(struct vme_resource *resource)
  1325. {
  1326. struct vme_lm_resource *lm;
  1327. if (resource->type != VME_LM) {
  1328. printk(KERN_ERR "Not a Location Monitor resource\n");
  1329. return -EINVAL;
  1330. }
  1331. lm = list_entry(resource->entry, struct vme_lm_resource, list);
  1332. return lm->monitors;
  1333. }
  1334. EXPORT_SYMBOL(vme_lm_count);
  1335. /**
  1336. * vme_lm_set - Configure location monitor
  1337. * @resource: Pointer to VME location monitor resource.
  1338. * @lm_base: Base address to monitor.
  1339. * @aspace: VME address space to monitor.
  1340. * @cycle: VME bus cycle type to monitor.
  1341. *
  1342. * Set the base address, address space and cycle type of accesses to be
  1343. * monitored by the location monitor.
  1344. *
  1345. * Return: Zero on success, -EINVAL when provided with an invalid location
  1346. * monitor resource or function is not supported. Hardware specific
  1347. * errors may also be returned.
  1348. */
  1349. int vme_lm_set(struct vme_resource *resource, unsigned long long lm_base,
  1350. u32 aspace, u32 cycle)
  1351. {
  1352. struct vme_bridge *bridge = find_bridge(resource);
  1353. struct vme_lm_resource *lm;
  1354. if (resource->type != VME_LM) {
  1355. printk(KERN_ERR "Not a Location Monitor resource\n");
  1356. return -EINVAL;
  1357. }
  1358. lm = list_entry(resource->entry, struct vme_lm_resource, list);
  1359. if (!bridge->lm_set) {
  1360. printk(KERN_ERR "vme_lm_set not supported\n");
  1361. return -EINVAL;
  1362. }
  1363. return bridge->lm_set(lm, lm_base, aspace, cycle);
  1364. }
  1365. EXPORT_SYMBOL(vme_lm_set);
  1366. /**
  1367. * vme_lm_get - Retrieve location monitor settings
  1368. * @resource: Pointer to VME location monitor resource.
  1369. * @lm_base: Pointer used to output the base address monitored.
  1370. * @aspace: Pointer used to output the address space monitored.
  1371. * @cycle: Pointer used to output the VME bus cycle type monitored.
  1372. *
  1373. * Retrieve the base address, address space and cycle type of accesses to
  1374. * be monitored by the location monitor.
  1375. *
  1376. * Return: Zero on success, -EINVAL when provided with an invalid location
  1377. * monitor resource or function is not supported. Hardware specific
  1378. * errors may also be returned.
  1379. */
  1380. int vme_lm_get(struct vme_resource *resource, unsigned long long *lm_base,
  1381. u32 *aspace, u32 *cycle)
  1382. {
  1383. struct vme_bridge *bridge = find_bridge(resource);
  1384. struct vme_lm_resource *lm;
  1385. if (resource->type != VME_LM) {
  1386. printk(KERN_ERR "Not a Location Monitor resource\n");
  1387. return -EINVAL;
  1388. }
  1389. lm = list_entry(resource->entry, struct vme_lm_resource, list);
  1390. if (!bridge->lm_get) {
  1391. printk(KERN_ERR "vme_lm_get not supported\n");
  1392. return -EINVAL;
  1393. }
  1394. return bridge->lm_get(lm, lm_base, aspace, cycle);
  1395. }
  1396. EXPORT_SYMBOL(vme_lm_get);
  1397. /**
  1398. * vme_lm_attach - Provide callback for location monitor address
  1399. * @resource: Pointer to VME location monitor resource.
  1400. * @monitor: Offset to which callback should be attached.
  1401. * @callback: Pointer to callback function called when triggered.
  1402. * @data: Generic pointer that will be passed to the callback function.
  1403. *
  1404. * Attach a callback to the specificed offset into the location monitors
  1405. * monitored addresses. A generic pointer is provided to allow data to be
  1406. * passed to the callback when called.
  1407. *
  1408. * Return: Zero on success, -EINVAL when provided with an invalid location
  1409. * monitor resource or function is not supported. Hardware specific
  1410. * errors may also be returned.
  1411. */
  1412. int vme_lm_attach(struct vme_resource *resource, int monitor,
  1413. void (*callback)(void *), void *data)
  1414. {
  1415. struct vme_bridge *bridge = find_bridge(resource);
  1416. struct vme_lm_resource *lm;
  1417. if (resource->type != VME_LM) {
  1418. printk(KERN_ERR "Not a Location Monitor resource\n");
  1419. return -EINVAL;
  1420. }
  1421. lm = list_entry(resource->entry, struct vme_lm_resource, list);
  1422. if (!bridge->lm_attach) {
  1423. printk(KERN_ERR "vme_lm_attach not supported\n");
  1424. return -EINVAL;
  1425. }
  1426. return bridge->lm_attach(lm, monitor, callback, data);
  1427. }
  1428. EXPORT_SYMBOL(vme_lm_attach);
  1429. /**
  1430. * vme_lm_detach - Remove callback for location monitor address
  1431. * @resource: Pointer to VME location monitor resource.
  1432. * @monitor: Offset to which callback should be removed.
  1433. *
  1434. * Remove the callback associated with the specificed offset into the
  1435. * location monitors monitored addresses.
  1436. *
  1437. * Return: Zero on success, -EINVAL when provided with an invalid location
  1438. * monitor resource or function is not supported. Hardware specific
  1439. * errors may also be returned.
  1440. */
  1441. int vme_lm_detach(struct vme_resource *resource, int monitor)
  1442. {
  1443. struct vme_bridge *bridge = find_bridge(resource);
  1444. struct vme_lm_resource *lm;
  1445. if (resource->type != VME_LM) {
  1446. printk(KERN_ERR "Not a Location Monitor resource\n");
  1447. return -EINVAL;
  1448. }
  1449. lm = list_entry(resource->entry, struct vme_lm_resource, list);
  1450. if (!bridge->lm_detach) {
  1451. printk(KERN_ERR "vme_lm_detach not supported\n");
  1452. return -EINVAL;
  1453. }
  1454. return bridge->lm_detach(lm, monitor);
  1455. }
  1456. EXPORT_SYMBOL(vme_lm_detach);
  1457. /**
  1458. * vme_lm_free - Free allocated VME location monitor
  1459. * @resource: Pointer to VME location monitor resource.
  1460. *
  1461. * Free allocation of a VME location monitor.
  1462. *
  1463. * WARNING: This function currently expects that any callbacks that have
  1464. * been attached to the location monitor have been removed.
  1465. *
  1466. * Return: Zero on success, -EINVAL when provided with an invalid location
  1467. * monitor resource.
  1468. */
  1469. void vme_lm_free(struct vme_resource *resource)
  1470. {
  1471. struct vme_lm_resource *lm;
  1472. if (resource->type != VME_LM) {
  1473. printk(KERN_ERR "Not a Location Monitor resource\n");
  1474. return;
  1475. }
  1476. lm = list_entry(resource->entry, struct vme_lm_resource, list);
  1477. mutex_lock(&lm->mtx);
  1478. /* XXX
  1479. * Check to see that there aren't any callbacks still attached, if
  1480. * there are we should probably be detaching them!
  1481. */
  1482. lm->locked = 0;
  1483. mutex_unlock(&lm->mtx);
  1484. kfree(resource);
  1485. }
  1486. EXPORT_SYMBOL(vme_lm_free);
  1487. /**
  1488. * vme_slot_num - Retrieve slot ID
  1489. * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
  1490. *
  1491. * Retrieve the slot ID associated with the provided VME device.
  1492. *
  1493. * Return: The slot ID on success, -EINVAL if VME bridge cannot be determined
  1494. * or the function is not supported. Hardware specific errors may also
  1495. * be returned.
  1496. */
  1497. int vme_slot_num(struct vme_dev *vdev)
  1498. {
  1499. struct vme_bridge *bridge;
  1500. bridge = vdev->bridge;
  1501. if (!bridge) {
  1502. printk(KERN_ERR "Can't find VME bus\n");
  1503. return -EINVAL;
  1504. }
  1505. if (!bridge->slot_get) {
  1506. printk(KERN_WARNING "vme_slot_num not supported\n");
  1507. return -EINVAL;
  1508. }
  1509. return bridge->slot_get(bridge);
  1510. }
  1511. EXPORT_SYMBOL(vme_slot_num);
  1512. /**
  1513. * vme_bus_num - Retrieve bus number
  1514. * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
  1515. *
  1516. * Retrieve the bus enumeration associated with the provided VME device.
  1517. *
  1518. * Return: The bus number on success, -EINVAL if VME bridge cannot be
  1519. * determined.
  1520. */
  1521. int vme_bus_num(struct vme_dev *vdev)
  1522. {
  1523. struct vme_bridge *bridge;
  1524. bridge = vdev->bridge;
  1525. if (!bridge) {
  1526. pr_err("Can't find VME bus\n");
  1527. return -EINVAL;
  1528. }
  1529. return bridge->num;
  1530. }
  1531. EXPORT_SYMBOL(vme_bus_num);
  1532. /* - Bridge Registration --------------------------------------------------- */
  1533. static void vme_dev_release(struct device *dev)
  1534. {
  1535. kfree(dev_to_vme_dev(dev));
  1536. }
  1537. /* Common bridge initialization */
  1538. struct vme_bridge *vme_init_bridge(struct vme_bridge *bridge)
  1539. {
  1540. INIT_LIST_HEAD(&bridge->vme_error_handlers);
  1541. INIT_LIST_HEAD(&bridge->master_resources);
  1542. INIT_LIST_HEAD(&bridge->slave_resources);
  1543. INIT_LIST_HEAD(&bridge->dma_resources);
  1544. INIT_LIST_HEAD(&bridge->lm_resources);
  1545. mutex_init(&bridge->irq_mtx);
  1546. return bridge;
  1547. }
  1548. EXPORT_SYMBOL(vme_init_bridge);
  1549. int vme_register_bridge(struct vme_bridge *bridge)
  1550. {
  1551. int i;
  1552. int ret = -1;
  1553. mutex_lock(&vme_buses_lock);
  1554. for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) {
  1555. if ((vme_bus_numbers & (1 << i)) == 0) {
  1556. vme_bus_numbers |= (1 << i);
  1557. bridge->num = i;
  1558. INIT_LIST_HEAD(&bridge->devices);
  1559. list_add_tail(&bridge->bus_list, &vme_bus_list);
  1560. ret = 0;
  1561. break;
  1562. }
  1563. }
  1564. mutex_unlock(&vme_buses_lock);
  1565. return ret;
  1566. }
  1567. EXPORT_SYMBOL(vme_register_bridge);
  1568. void vme_unregister_bridge(struct vme_bridge *bridge)
  1569. {
  1570. struct vme_dev *vdev;
  1571. struct vme_dev *tmp;
  1572. mutex_lock(&vme_buses_lock);
  1573. vme_bus_numbers &= ~(1 << bridge->num);
  1574. list_for_each_entry_safe(vdev, tmp, &bridge->devices, bridge_list) {
  1575. list_del(&vdev->drv_list);
  1576. list_del(&vdev->bridge_list);
  1577. device_unregister(&vdev->dev);
  1578. }
  1579. list_del(&bridge->bus_list);
  1580. mutex_unlock(&vme_buses_lock);
  1581. }
  1582. EXPORT_SYMBOL(vme_unregister_bridge);
  1583. /* - Driver Registration --------------------------------------------------- */
  1584. static int __vme_register_driver_bus(struct vme_driver *drv,
  1585. struct vme_bridge *bridge, unsigned int ndevs)
  1586. {
  1587. int err;
  1588. unsigned int i;
  1589. struct vme_dev *vdev;
  1590. struct vme_dev *tmp;
  1591. for (i = 0; i < ndevs; i++) {
  1592. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  1593. if (!vdev) {
  1594. err = -ENOMEM;
  1595. goto err_devalloc;
  1596. }
  1597. vdev->num = i;
  1598. vdev->bridge = bridge;
  1599. vdev->dev.platform_data = drv;
  1600. vdev->dev.release = vme_dev_release;
  1601. vdev->dev.parent = bridge->parent;
  1602. vdev->dev.bus = &vme_bus_type;
  1603. dev_set_name(&vdev->dev, "%s.%u-%u", drv->name, bridge->num,
  1604. vdev->num);
  1605. err = device_register(&vdev->dev);
  1606. if (err)
  1607. goto err_reg;
  1608. if (vdev->dev.platform_data) {
  1609. list_add_tail(&vdev->drv_list, &drv->devices);
  1610. list_add_tail(&vdev->bridge_list, &bridge->devices);
  1611. } else
  1612. device_unregister(&vdev->dev);
  1613. }
  1614. return 0;
  1615. err_reg:
  1616. put_device(&vdev->dev);
  1617. kfree(vdev);
  1618. err_devalloc:
  1619. list_for_each_entry_safe(vdev, tmp, &drv->devices, drv_list) {
  1620. list_del(&vdev->drv_list);
  1621. list_del(&vdev->bridge_list);
  1622. device_unregister(&vdev->dev);
  1623. }
  1624. return err;
  1625. }
  1626. static int __vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
  1627. {
  1628. struct vme_bridge *bridge;
  1629. int err = 0;
  1630. mutex_lock(&vme_buses_lock);
  1631. list_for_each_entry(bridge, &vme_bus_list, bus_list) {
  1632. /*
  1633. * This cannot cause trouble as we already have vme_buses_lock
  1634. * and if the bridge is removed, it will have to go through
  1635. * vme_unregister_bridge() to do it (which calls remove() on
  1636. * the bridge which in turn tries to acquire vme_buses_lock and
  1637. * will have to wait).
  1638. */
  1639. err = __vme_register_driver_bus(drv, bridge, ndevs);
  1640. if (err)
  1641. break;
  1642. }
  1643. mutex_unlock(&vme_buses_lock);
  1644. return err;
  1645. }
  1646. /**
  1647. * vme_register_driver - Register a VME driver
  1648. * @drv: Pointer to VME driver structure to register.
  1649. * @ndevs: Maximum number of devices to allow to be enumerated.
  1650. *
  1651. * Register a VME device driver with the VME subsystem.
  1652. *
  1653. * Return: Zero on success, error value on registration failure.
  1654. */
  1655. int vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
  1656. {
  1657. int err;
  1658. drv->driver.name = drv->name;
  1659. drv->driver.bus = &vme_bus_type;
  1660. INIT_LIST_HEAD(&drv->devices);
  1661. err = driver_register(&drv->driver);
  1662. if (err)
  1663. return err;
  1664. err = __vme_register_driver(drv, ndevs);
  1665. if (err)
  1666. driver_unregister(&drv->driver);
  1667. return err;
  1668. }
  1669. EXPORT_SYMBOL(vme_register_driver);
  1670. /**
  1671. * vme_unregister_driver - Unregister a VME driver
  1672. * @drv: Pointer to VME driver structure to unregister.
  1673. *
  1674. * Unregister a VME device driver from the VME subsystem.
  1675. */
  1676. void vme_unregister_driver(struct vme_driver *drv)
  1677. {
  1678. struct vme_dev *dev, *dev_tmp;
  1679. mutex_lock(&vme_buses_lock);
  1680. list_for_each_entry_safe(dev, dev_tmp, &drv->devices, drv_list) {
  1681. list_del(&dev->drv_list);
  1682. list_del(&dev->bridge_list);
  1683. device_unregister(&dev->dev);
  1684. }
  1685. mutex_unlock(&vme_buses_lock);
  1686. driver_unregister(&drv->driver);
  1687. }
  1688. EXPORT_SYMBOL(vme_unregister_driver);
  1689. /* - Bus Registration ------------------------------------------------------ */
  1690. static int vme_bus_match(struct device *dev, struct device_driver *drv)
  1691. {
  1692. struct vme_driver *vme_drv;
  1693. vme_drv = container_of(drv, struct vme_driver, driver);
  1694. if (dev->platform_data == vme_drv) {
  1695. struct vme_dev *vdev = dev_to_vme_dev(dev);
  1696. if (vme_drv->match && vme_drv->match(vdev))
  1697. return 1;
  1698. dev->platform_data = NULL;
  1699. }
  1700. return 0;
  1701. }
  1702. static int vme_bus_probe(struct device *dev)
  1703. {
  1704. struct vme_driver *driver;
  1705. struct vme_dev *vdev = dev_to_vme_dev(dev);
  1706. driver = dev->platform_data;
  1707. if (driver->probe)
  1708. return driver->probe(vdev);
  1709. return -ENODEV;
  1710. }
  1711. static int vme_bus_remove(struct device *dev)
  1712. {
  1713. struct vme_driver *driver;
  1714. struct vme_dev *vdev = dev_to_vme_dev(dev);
  1715. driver = dev->platform_data;
  1716. if (driver->remove)
  1717. return driver->remove(vdev);
  1718. return -ENODEV;
  1719. }
  1720. struct bus_type vme_bus_type = {
  1721. .name = "vme",
  1722. .match = vme_bus_match,
  1723. .probe = vme_bus_probe,
  1724. .remove = vme_bus_remove,
  1725. };
  1726. EXPORT_SYMBOL(vme_bus_type);
  1727. static int __init vme_init(void)
  1728. {
  1729. return bus_register(&vme_bus_type);
  1730. }
  1731. subsys_initcall(vme_init);