maple.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. /*
  2. * Core maple bus functionality
  3. *
  4. * Copyright (C) 2007, 2008 Adrian McMenamin
  5. * Copyright (C) 2001 - 2008 Paul Mundt
  6. *
  7. * Based on 2.4 code by:
  8. *
  9. * Copyright (C) 2000-2001 YAEGASHI Takeshi
  10. * Copyright (C) 2001 M. R. Brown
  11. * Copyright (C) 2001 Paul Mundt
  12. *
  13. * and others.
  14. *
  15. * This file is subject to the terms and conditions of the GNU General Public
  16. * License. See the file "COPYING" in the main directory of this archive
  17. * for more details.
  18. */
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/device.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/list.h>
  24. #include <linux/io.h>
  25. #include <linux/slab.h>
  26. #include <linux/maple.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/delay.h>
  29. #include <asm/cacheflush.h>
  30. #include <asm/dma.h>
  31. #include <asm/io.h>
  32. #include <mach/dma.h>
  33. #include <mach/sysasic.h>
  34. MODULE_AUTHOR("Yaegashi Takeshi, Paul Mundt, M. R. Brown, Adrian McMenamin");
  35. MODULE_DESCRIPTION("Maple bus driver for Dreamcast");
  36. MODULE_LICENSE("GPL v2");
  37. MODULE_SUPPORTED_DEVICE("{{SEGA, Dreamcast/Maple}}");
  38. static void maple_dma_handler(struct work_struct *work);
  39. static void maple_vblank_handler(struct work_struct *work);
  40. static DECLARE_WORK(maple_dma_process, maple_dma_handler);
  41. static DECLARE_WORK(maple_vblank_process, maple_vblank_handler);
  42. static LIST_HEAD(maple_waitq);
  43. static LIST_HEAD(maple_sentq);
  44. /* mutex to protect queue of waiting packets */
  45. static DEFINE_MUTEX(maple_wlist_lock);
  46. static struct maple_driver maple_dummy_driver;
  47. static struct device maple_bus;
  48. static int subdevice_map[MAPLE_PORTS];
  49. static unsigned long *maple_sendbuf, *maple_sendptr, *maple_lastptr;
  50. static unsigned long maple_pnp_time;
  51. static int started, scanning, fullscan;
  52. static struct kmem_cache *maple_queue_cache;
  53. struct maple_device_specify {
  54. int port;
  55. int unit;
  56. };
  57. static bool checked[4];
  58. static struct maple_device *baseunits[4];
  59. /**
  60. * maple_driver_register - register a maple driver
  61. * @drv: maple driver to be registered.
  62. *
  63. * Registers the passed in @drv, while updating the bus type.
  64. * Devices with matching function IDs will be automatically probed.
  65. */
  66. int maple_driver_register(struct maple_driver *drv)
  67. {
  68. if (!drv)
  69. return -EINVAL;
  70. drv->drv.bus = &maple_bus_type;
  71. return driver_register(&drv->drv);
  72. }
  73. EXPORT_SYMBOL_GPL(maple_driver_register);
  74. /**
  75. * maple_driver_unregister - unregister a maple driver.
  76. * @drv: maple driver to unregister.
  77. *
  78. * Cleans up after maple_driver_register(). To be invoked in the exit
  79. * path of any module drivers.
  80. */
  81. void maple_driver_unregister(struct maple_driver *drv)
  82. {
  83. driver_unregister(&drv->drv);
  84. }
  85. EXPORT_SYMBOL_GPL(maple_driver_unregister);
  86. /* set hardware registers to enable next round of dma */
  87. static void maplebus_dma_reset(void)
  88. {
  89. ctrl_outl(MAPLE_MAGIC, MAPLE_RESET);
  90. /* set trig type to 0 for software trigger, 1 for hardware (VBLANK) */
  91. ctrl_outl(1, MAPLE_TRIGTYPE);
  92. ctrl_outl(MAPLE_2MBPS | MAPLE_TIMEOUT(50000), MAPLE_SPEED);
  93. ctrl_outl(PHYSADDR(maple_sendbuf), MAPLE_DMAADDR);
  94. ctrl_outl(1, MAPLE_ENABLE);
  95. }
  96. /**
  97. * maple_getcond_callback - setup handling MAPLE_COMMAND_GETCOND
  98. * @dev: device responding
  99. * @callback: handler callback
  100. * @interval: interval in jiffies between callbacks
  101. * @function: the function code for the device
  102. */
  103. void maple_getcond_callback(struct maple_device *dev,
  104. void (*callback) (struct mapleq *mq),
  105. unsigned long interval, unsigned long function)
  106. {
  107. dev->callback = callback;
  108. dev->interval = interval;
  109. dev->function = cpu_to_be32(function);
  110. dev->when = jiffies;
  111. }
  112. EXPORT_SYMBOL_GPL(maple_getcond_callback);
  113. static int maple_dma_done(void)
  114. {
  115. return (ctrl_inl(MAPLE_STATE) & 1) == 0;
  116. }
  117. static void maple_release_device(struct device *dev)
  118. {
  119. struct maple_device *mdev;
  120. struct mapleq *mq;
  121. if (!dev)
  122. return;
  123. mdev = to_maple_dev(dev);
  124. mq = mdev->mq;
  125. if (mq) {
  126. if (mq->recvbufdcsp)
  127. kmem_cache_free(maple_queue_cache, mq->recvbufdcsp);
  128. kfree(mq);
  129. mq = NULL;
  130. }
  131. kfree(mdev);
  132. }
  133. /**
  134. * maple_add_packet - add a single instruction to the queue
  135. * @mdev: maple device
  136. * @function: function on device being queried
  137. * @command: maple command to add
  138. * @length: length of command string (in 32 bit words)
  139. * @data: remainder of command string
  140. */
  141. int maple_add_packet(struct maple_device *mdev, u32 function, u32 command,
  142. size_t length, void *data)
  143. {
  144. int locking, ret = 0;
  145. void *sendbuf = NULL;
  146. mutex_lock(&maple_wlist_lock);
  147. /* bounce if device already locked */
  148. locking = mutex_is_locked(&mdev->mq->mutex);
  149. if (locking) {
  150. ret = -EBUSY;
  151. goto out;
  152. }
  153. mutex_lock(&mdev->mq->mutex);
  154. if (length) {
  155. sendbuf = kmalloc(length * 4, GFP_KERNEL);
  156. if (!sendbuf) {
  157. mutex_unlock(&mdev->mq->mutex);
  158. ret = -ENOMEM;
  159. goto out;
  160. }
  161. ((__be32 *)sendbuf)[0] = cpu_to_be32(function);
  162. }
  163. mdev->mq->command = command;
  164. mdev->mq->length = length;
  165. if (length > 1)
  166. memcpy(sendbuf + 4, data, (length - 1) * 4);
  167. mdev->mq->sendbuf = sendbuf;
  168. list_add(&mdev->mq->list, &maple_waitq);
  169. out:
  170. mutex_unlock(&maple_wlist_lock);
  171. return ret;
  172. }
  173. EXPORT_SYMBOL_GPL(maple_add_packet);
  174. /**
  175. * maple_add_packet_sleeps - add a single instruction to the queue
  176. * @mdev: maple device
  177. * @function: function on device being queried
  178. * @command: maple command to add
  179. * @length: length of command string (in 32 bit words)
  180. * @data: remainder of command string
  181. *
  182. * Same as maple_add_packet(), but waits for the lock to become free.
  183. */
  184. int maple_add_packet_sleeps(struct maple_device *mdev, u32 function,
  185. u32 command, size_t length, void *data)
  186. {
  187. int locking, ret = 0;
  188. void *sendbuf = NULL;
  189. locking = mutex_lock_interruptible(&mdev->mq->mutex);
  190. if (locking) {
  191. ret = -EIO;
  192. goto out;
  193. }
  194. if (length) {
  195. sendbuf = kmalloc(length * 4, GFP_KERNEL);
  196. if (!sendbuf) {
  197. mutex_unlock(&mdev->mq->mutex);
  198. ret = -ENOMEM;
  199. goto out;
  200. }
  201. ((__be32 *)sendbuf)[0] = cpu_to_be32(function);
  202. }
  203. mdev->mq->command = command;
  204. mdev->mq->length = length;
  205. if (length > 1)
  206. memcpy(sendbuf + 4, data, (length - 1) * 4);
  207. mdev->mq->sendbuf = sendbuf;
  208. mutex_lock(&maple_wlist_lock);
  209. list_add(&mdev->mq->list, &maple_waitq);
  210. mutex_unlock(&maple_wlist_lock);
  211. out:
  212. return ret;
  213. }
  214. EXPORT_SYMBOL_GPL(maple_add_packet_sleeps);
  215. static struct mapleq *maple_allocq(struct maple_device *mdev)
  216. {
  217. struct mapleq *mq;
  218. mq = kmalloc(sizeof(*mq), GFP_KERNEL);
  219. if (!mq)
  220. goto failed_nomem;
  221. mq->dev = mdev;
  222. mq->recvbufdcsp = kmem_cache_zalloc(maple_queue_cache, GFP_KERNEL);
  223. mq->recvbuf = (void *) P2SEGADDR(mq->recvbufdcsp);
  224. if (!mq->recvbuf)
  225. goto failed_p2;
  226. /*
  227. * most devices do not need the mutex - but
  228. * anything that injects block reads or writes
  229. * will rely on it
  230. */
  231. mutex_init(&mq->mutex);
  232. return mq;
  233. failed_p2:
  234. kfree(mq);
  235. failed_nomem:
  236. return NULL;
  237. }
  238. static struct maple_device *maple_alloc_dev(int port, int unit)
  239. {
  240. struct maple_device *mdev;
  241. mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
  242. if (!mdev)
  243. return NULL;
  244. mdev->port = port;
  245. mdev->unit = unit;
  246. mdev->mq = maple_allocq(mdev);
  247. if (!mdev->mq) {
  248. kfree(mdev);
  249. return NULL;
  250. }
  251. mdev->dev.bus = &maple_bus_type;
  252. mdev->dev.parent = &maple_bus;
  253. return mdev;
  254. }
  255. static void maple_free_dev(struct maple_device *mdev)
  256. {
  257. if (!mdev)
  258. return;
  259. if (mdev->mq) {
  260. if (mdev->mq->recvbufdcsp)
  261. kmem_cache_free(maple_queue_cache,
  262. mdev->mq->recvbufdcsp);
  263. kfree(mdev->mq);
  264. }
  265. kfree(mdev);
  266. }
  267. /* process the command queue into a maple command block
  268. * terminating command has bit 32 of first long set to 0
  269. */
  270. static void maple_build_block(struct mapleq *mq)
  271. {
  272. int port, unit, from, to, len;
  273. unsigned long *lsendbuf = mq->sendbuf;
  274. port = mq->dev->port & 3;
  275. unit = mq->dev->unit;
  276. len = mq->length;
  277. from = port << 6;
  278. to = (port << 6) | (unit > 0 ? (1 << (unit - 1)) & 0x1f : 0x20);
  279. *maple_lastptr &= 0x7fffffff;
  280. maple_lastptr = maple_sendptr;
  281. *maple_sendptr++ = (port << 16) | len | 0x80000000;
  282. *maple_sendptr++ = PHYSADDR(mq->recvbuf);
  283. *maple_sendptr++ =
  284. mq->command | (to << 8) | (from << 16) | (len << 24);
  285. while (len-- > 0)
  286. *maple_sendptr++ = *lsendbuf++;
  287. }
  288. /* build up command queue */
  289. static void maple_send(void)
  290. {
  291. int i, maple_packets = 0;
  292. struct mapleq *mq, *nmq;
  293. if (!list_empty(&maple_sentq))
  294. return;
  295. mutex_lock(&maple_wlist_lock);
  296. if (list_empty(&maple_waitq) || !maple_dma_done()) {
  297. mutex_unlock(&maple_wlist_lock);
  298. return;
  299. }
  300. mutex_unlock(&maple_wlist_lock);
  301. maple_lastptr = maple_sendbuf;
  302. maple_sendptr = maple_sendbuf;
  303. mutex_lock(&maple_wlist_lock);
  304. list_for_each_entry_safe(mq, nmq, &maple_waitq, list) {
  305. maple_build_block(mq);
  306. list_move(&mq->list, &maple_sentq);
  307. if (maple_packets++ > MAPLE_MAXPACKETS)
  308. break;
  309. }
  310. mutex_unlock(&maple_wlist_lock);
  311. if (maple_packets > 0) {
  312. for (i = 0; i < (1 << MAPLE_DMA_PAGES); i++)
  313. dma_cache_sync(0, maple_sendbuf + i * PAGE_SIZE,
  314. PAGE_SIZE, DMA_BIDIRECTIONAL);
  315. }
  316. }
  317. /* check if there is a driver registered likely to match this device */
  318. static int check_matching_maple_driver(struct device_driver *driver,
  319. void *devptr)
  320. {
  321. struct maple_driver *maple_drv;
  322. struct maple_device *mdev;
  323. mdev = devptr;
  324. maple_drv = to_maple_driver(driver);
  325. if (mdev->devinfo.function & cpu_to_be32(maple_drv->function))
  326. return 1;
  327. return 0;
  328. }
  329. static void maple_detach_driver(struct maple_device *mdev)
  330. {
  331. if (!mdev)
  332. return;
  333. device_unregister(&mdev->dev);
  334. mdev = NULL;
  335. }
  336. /* process initial MAPLE_COMMAND_DEVINFO for each device or port */
  337. static void maple_attach_driver(struct maple_device *mdev)
  338. {
  339. char *p, *recvbuf;
  340. unsigned long function;
  341. int matched, retval;
  342. recvbuf = mdev->mq->recvbuf;
  343. /* copy the data as individual elements in
  344. * case of memory optimisation */
  345. memcpy(&mdev->devinfo.function, recvbuf + 4, 4);
  346. memcpy(&mdev->devinfo.function_data[0], recvbuf + 8, 12);
  347. memcpy(&mdev->devinfo.area_code, recvbuf + 20, 1);
  348. memcpy(&mdev->devinfo.connector_direction, recvbuf + 21, 1);
  349. memcpy(&mdev->devinfo.product_name[0], recvbuf + 22, 30);
  350. memcpy(&mdev->devinfo.product_licence[0], recvbuf + 52, 60);
  351. memcpy(&mdev->devinfo.standby_power, recvbuf + 112, 2);
  352. memcpy(&mdev->devinfo.max_power, recvbuf + 114, 2);
  353. memcpy(mdev->product_name, mdev->devinfo.product_name, 30);
  354. mdev->product_name[30] = '\0';
  355. memcpy(mdev->product_licence, mdev->devinfo.product_licence, 60);
  356. mdev->product_licence[60] = '\0';
  357. for (p = mdev->product_name + 29; mdev->product_name <= p; p--)
  358. if (*p == ' ')
  359. *p = '\0';
  360. else
  361. break;
  362. for (p = mdev->product_licence + 59; mdev->product_licence <= p; p--)
  363. if (*p == ' ')
  364. *p = '\0';
  365. else
  366. break;
  367. printk(KERN_INFO "Maple device detected: %s\n",
  368. mdev->product_name);
  369. printk(KERN_INFO "Maple device: %s\n", mdev->product_licence);
  370. function = be32_to_cpu(mdev->devinfo.function);
  371. if (function > 0x200) {
  372. /* Do this silently - as not a real device */
  373. function = 0;
  374. mdev->driver = &maple_dummy_driver;
  375. sprintf(mdev->dev.bus_id, "%d:0.port", mdev->port);
  376. } else {
  377. printk(KERN_INFO
  378. "Maple bus at (%d, %d): Function 0x%lX\n",
  379. mdev->port, mdev->unit, function);
  380. matched =
  381. bus_for_each_drv(&maple_bus_type, NULL, mdev,
  382. check_matching_maple_driver);
  383. if (matched == 0) {
  384. /* Driver does not exist yet */
  385. printk(KERN_INFO
  386. "No maple driver found.\n");
  387. mdev->driver = &maple_dummy_driver;
  388. }
  389. sprintf(mdev->dev.bus_id, "%d:0%d.%lX", mdev->port,
  390. mdev->unit, function);
  391. }
  392. mdev->function = function;
  393. mdev->dev.release = &maple_release_device;
  394. retval = device_register(&mdev->dev);
  395. if (retval) {
  396. printk(KERN_INFO
  397. "Maple bus: Attempt to register device"
  398. " (%x, %x) failed.\n",
  399. mdev->port, mdev->unit);
  400. maple_free_dev(mdev);
  401. mdev = NULL;
  402. return;
  403. }
  404. }
  405. /*
  406. * if device has been registered for the given
  407. * port and unit then return 1 - allows identification
  408. * of which devices need to be attached or detached
  409. */
  410. static int detach_maple_device(struct device *device, void *portptr)
  411. {
  412. struct maple_device_specify *ds;
  413. struct maple_device *mdev;
  414. ds = portptr;
  415. mdev = to_maple_dev(device);
  416. if (mdev->port == ds->port && mdev->unit == ds->unit)
  417. return 1;
  418. return 0;
  419. }
  420. static int setup_maple_commands(struct device *device, void *ignored)
  421. {
  422. int add;
  423. struct maple_device *maple_dev = to_maple_dev(device);
  424. if ((maple_dev->interval > 0)
  425. && time_after(jiffies, maple_dev->when)) {
  426. /* bounce if we cannot lock */
  427. add = maple_add_packet(maple_dev,
  428. be32_to_cpu(maple_dev->devinfo.function),
  429. MAPLE_COMMAND_GETCOND, 1, NULL);
  430. if (!add)
  431. maple_dev->when = jiffies + maple_dev->interval;
  432. } else {
  433. if (time_after(jiffies, maple_pnp_time))
  434. /* This will also bounce */
  435. maple_add_packet(maple_dev, 0,
  436. MAPLE_COMMAND_DEVINFO, 0, NULL);
  437. }
  438. return 0;
  439. }
  440. /* VBLANK bottom half - implemented via workqueue */
  441. static void maple_vblank_handler(struct work_struct *work)
  442. {
  443. if (!list_empty(&maple_sentq) || !maple_dma_done())
  444. return;
  445. ctrl_outl(0, MAPLE_ENABLE);
  446. bus_for_each_dev(&maple_bus_type, NULL, NULL,
  447. setup_maple_commands);
  448. if (time_after(jiffies, maple_pnp_time))
  449. maple_pnp_time = jiffies + MAPLE_PNP_INTERVAL;
  450. mutex_lock(&maple_wlist_lock);
  451. if (!list_empty(&maple_waitq) && list_empty(&maple_sentq)) {
  452. mutex_unlock(&maple_wlist_lock);
  453. maple_send();
  454. } else {
  455. mutex_unlock(&maple_wlist_lock);
  456. }
  457. maplebus_dma_reset();
  458. }
  459. /* handle devices added via hotplugs - placing them on queue for DEVINFO*/
  460. static void maple_map_subunits(struct maple_device *mdev, int submask)
  461. {
  462. int retval, k, devcheck;
  463. struct maple_device *mdev_add;
  464. struct maple_device_specify ds;
  465. ds.port = mdev->port;
  466. for (k = 0; k < 5; k++) {
  467. ds.unit = k + 1;
  468. retval =
  469. bus_for_each_dev(&maple_bus_type, NULL, &ds,
  470. detach_maple_device);
  471. if (retval) {
  472. submask = submask >> 1;
  473. continue;
  474. }
  475. devcheck = submask & 0x01;
  476. if (devcheck) {
  477. mdev_add = maple_alloc_dev(mdev->port, k + 1);
  478. if (!mdev_add)
  479. return;
  480. maple_add_packet(mdev_add, 0, MAPLE_COMMAND_DEVINFO,
  481. 0, NULL);
  482. /* mark that we are checking sub devices */
  483. scanning = 1;
  484. }
  485. submask = submask >> 1;
  486. }
  487. }
  488. /* mark a device as removed */
  489. static void maple_clean_submap(struct maple_device *mdev)
  490. {
  491. int killbit;
  492. killbit = (mdev->unit > 0 ? (1 << (mdev->unit - 1)) & 0x1f : 0x20);
  493. killbit = ~killbit;
  494. killbit &= 0xFF;
  495. subdevice_map[mdev->port] = subdevice_map[mdev->port] & killbit;
  496. }
  497. /* handle empty port or hotplug removal */
  498. static void maple_response_none(struct maple_device *mdev,
  499. struct mapleq *mq)
  500. {
  501. if (mdev->unit != 0) {
  502. list_del(&mq->list);
  503. maple_clean_submap(mdev);
  504. printk(KERN_INFO
  505. "Maple bus device detaching at (%d, %d)\n",
  506. mdev->port, mdev->unit);
  507. maple_detach_driver(mdev);
  508. return;
  509. }
  510. if (!started || !fullscan) {
  511. if (checked[mdev->port] == false) {
  512. checked[mdev->port] = true;
  513. printk(KERN_INFO "No maple devices attached"
  514. " to port %d\n", mdev->port);
  515. }
  516. return;
  517. }
  518. maple_clean_submap(mdev);
  519. }
  520. /* preprocess hotplugs or scans */
  521. static void maple_response_devinfo(struct maple_device *mdev,
  522. char *recvbuf)
  523. {
  524. char submask;
  525. if (!started || (scanning == 2) || !fullscan) {
  526. if ((mdev->unit == 0) && (checked[mdev->port] == false)) {
  527. checked[mdev->port] = true;
  528. maple_attach_driver(mdev);
  529. } else {
  530. if (mdev->unit != 0)
  531. maple_attach_driver(mdev);
  532. }
  533. return;
  534. }
  535. if (mdev->unit == 0) {
  536. submask = recvbuf[2] & 0x1F;
  537. if (submask ^ subdevice_map[mdev->port]) {
  538. maple_map_subunits(mdev, submask);
  539. subdevice_map[mdev->port] = submask;
  540. }
  541. }
  542. }
  543. static void maple_port_rescan(void)
  544. {
  545. int i;
  546. struct maple_device *mdev;
  547. fullscan = 1;
  548. for (i = 0; i < MAPLE_PORTS; i++) {
  549. if (checked[i] == false) {
  550. fullscan = 0;
  551. mdev = baseunits[i];
  552. /*
  553. * test lock in case scan has failed
  554. * but device is still locked
  555. */
  556. if (mutex_is_locked(&mdev->mq->mutex))
  557. mutex_unlock(&mdev->mq->mutex);
  558. maple_add_packet(mdev, 0, MAPLE_COMMAND_DEVINFO,
  559. 0, NULL);
  560. }
  561. }
  562. }
  563. /* maple dma end bottom half - implemented via workqueue */
  564. static void maple_dma_handler(struct work_struct *work)
  565. {
  566. struct mapleq *mq, *nmq;
  567. struct maple_device *dev;
  568. char *recvbuf;
  569. enum maple_code code;
  570. if (!maple_dma_done())
  571. return;
  572. ctrl_outl(0, MAPLE_ENABLE);
  573. if (!list_empty(&maple_sentq)) {
  574. list_for_each_entry_safe(mq, nmq, &maple_sentq, list) {
  575. recvbuf = mq->recvbuf;
  576. code = recvbuf[0];
  577. dev = mq->dev;
  578. kfree(mq->sendbuf);
  579. mutex_unlock(&mq->mutex);
  580. list_del_init(&mq->list);
  581. switch (code) {
  582. case MAPLE_RESPONSE_NONE:
  583. maple_response_none(dev, mq);
  584. break;
  585. case MAPLE_RESPONSE_DEVINFO:
  586. maple_response_devinfo(dev, recvbuf);
  587. break;
  588. case MAPLE_RESPONSE_DATATRF:
  589. if (dev->callback)
  590. dev->callback(mq);
  591. break;
  592. case MAPLE_RESPONSE_FILEERR:
  593. case MAPLE_RESPONSE_AGAIN:
  594. case MAPLE_RESPONSE_BADCMD:
  595. case MAPLE_RESPONSE_BADFUNC:
  596. printk(KERN_DEBUG
  597. "Maple non-fatal error 0x%X\n",
  598. code);
  599. break;
  600. case MAPLE_RESPONSE_ALLINFO:
  601. printk(KERN_DEBUG
  602. "Maple - extended device information"
  603. " not supported\n");
  604. break;
  605. case MAPLE_RESPONSE_OK:
  606. break;
  607. default:
  608. break;
  609. }
  610. }
  611. /* if scanning is 1 then we have subdevices to check */
  612. if (scanning == 1) {
  613. maple_send();
  614. scanning = 2;
  615. } else
  616. scanning = 0;
  617. /*check if we have actually tested all ports yet */
  618. if (!fullscan)
  619. maple_port_rescan();
  620. /* mark that we have been through the first scan */
  621. if (started == 0)
  622. started = 1;
  623. }
  624. maplebus_dma_reset();
  625. }
  626. static irqreturn_t maplebus_dma_interrupt(int irq, void *dev_id)
  627. {
  628. /* Load everything into the bottom half */
  629. schedule_work(&maple_dma_process);
  630. return IRQ_HANDLED;
  631. }
  632. static irqreturn_t maplebus_vblank_interrupt(int irq, void *dev_id)
  633. {
  634. schedule_work(&maple_vblank_process);
  635. return IRQ_HANDLED;
  636. }
  637. static int maple_set_dma_interrupt_handler(void)
  638. {
  639. return request_irq(HW_EVENT_MAPLE_DMA, maplebus_dma_interrupt,
  640. IRQF_SHARED, "maple bus DMA", &maple_dummy_driver);
  641. }
  642. static int maple_set_vblank_interrupt_handler(void)
  643. {
  644. return request_irq(HW_EVENT_VSYNC, maplebus_vblank_interrupt,
  645. IRQF_SHARED, "maple bus VBLANK", &maple_dummy_driver);
  646. }
  647. static int maple_get_dma_buffer(void)
  648. {
  649. maple_sendbuf =
  650. (void *) __get_free_pages(GFP_KERNEL | __GFP_ZERO,
  651. MAPLE_DMA_PAGES);
  652. if (!maple_sendbuf)
  653. return -ENOMEM;
  654. return 0;
  655. }
  656. static int match_maple_bus_driver(struct device *devptr,
  657. struct device_driver *drvptr)
  658. {
  659. struct maple_driver *maple_drv = to_maple_driver(drvptr);
  660. struct maple_device *maple_dev = to_maple_dev(devptr);
  661. /* Trap empty port case */
  662. if (maple_dev->devinfo.function == 0xFFFFFFFF)
  663. return 0;
  664. else if (maple_dev->devinfo.function &
  665. cpu_to_be32(maple_drv->function))
  666. return 1;
  667. return 0;
  668. }
  669. static int maple_bus_uevent(struct device *dev,
  670. struct kobj_uevent_env *env)
  671. {
  672. return 0;
  673. }
  674. static void maple_bus_release(struct device *dev)
  675. {
  676. }
  677. static struct maple_driver maple_dummy_driver = {
  678. .drv = {
  679. .name = "maple_dummy_driver",
  680. .bus = &maple_bus_type,
  681. },
  682. };
  683. struct bus_type maple_bus_type = {
  684. .name = "maple",
  685. .match = match_maple_bus_driver,
  686. .uevent = maple_bus_uevent,
  687. };
  688. EXPORT_SYMBOL_GPL(maple_bus_type);
  689. static struct device maple_bus = {
  690. .bus_id = "maple",
  691. .release = maple_bus_release,
  692. };
  693. static int __init maple_bus_init(void)
  694. {
  695. int retval, i;
  696. struct maple_device *mdev[MAPLE_PORTS];
  697. ctrl_outl(0, MAPLE_STATE);
  698. retval = device_register(&maple_bus);
  699. if (retval)
  700. goto cleanup;
  701. retval = bus_register(&maple_bus_type);
  702. if (retval)
  703. goto cleanup_device;
  704. retval = driver_register(&maple_dummy_driver.drv);
  705. if (retval)
  706. goto cleanup_bus;
  707. /* allocate memory for maple bus dma */
  708. retval = maple_get_dma_buffer();
  709. if (retval) {
  710. printk(KERN_INFO
  711. "Maple bus: Failed to allocate Maple DMA buffers\n");
  712. goto cleanup_basic;
  713. }
  714. /* set up DMA interrupt handler */
  715. retval = maple_set_dma_interrupt_handler();
  716. if (retval) {
  717. printk(KERN_INFO
  718. "Maple bus: Failed to grab maple DMA IRQ\n");
  719. goto cleanup_dma;
  720. }
  721. /* set up VBLANK interrupt handler */
  722. retval = maple_set_vblank_interrupt_handler();
  723. if (retval) {
  724. printk(KERN_INFO "Maple bus: Failed to grab VBLANK IRQ\n");
  725. goto cleanup_irq;
  726. }
  727. maple_queue_cache =
  728. kmem_cache_create("maple_queue_cache", 0x400, 0,
  729. SLAB_POISON|SLAB_HWCACHE_ALIGN, NULL);
  730. if (!maple_queue_cache)
  731. goto cleanup_bothirqs;
  732. INIT_LIST_HEAD(&maple_waitq);
  733. INIT_LIST_HEAD(&maple_sentq);
  734. /* setup maple ports */
  735. for (i = 0; i < MAPLE_PORTS; i++) {
  736. checked[i] = false;
  737. mdev[i] = maple_alloc_dev(i, 0);
  738. baseunits[i] = mdev[i];
  739. if (!mdev[i]) {
  740. while (i-- > 0)
  741. maple_free_dev(mdev[i]);
  742. goto cleanup_cache;
  743. }
  744. maple_add_packet(mdev[i], 0, MAPLE_COMMAND_DEVINFO, 0, NULL);
  745. subdevice_map[i] = 0;
  746. }
  747. /* setup maplebus hardware */
  748. maplebus_dma_reset();
  749. /* initial detection */
  750. maple_send();
  751. maple_pnp_time = jiffies;
  752. printk(KERN_INFO "Maple bus core now registered.\n");
  753. return 0;
  754. cleanup_cache:
  755. kmem_cache_destroy(maple_queue_cache);
  756. cleanup_bothirqs:
  757. free_irq(HW_EVENT_VSYNC, 0);
  758. cleanup_irq:
  759. free_irq(HW_EVENT_MAPLE_DMA, 0);
  760. cleanup_dma:
  761. free_pages((unsigned long) maple_sendbuf, MAPLE_DMA_PAGES);
  762. cleanup_basic:
  763. driver_unregister(&maple_dummy_driver.drv);
  764. cleanup_bus:
  765. bus_unregister(&maple_bus_type);
  766. cleanup_device:
  767. device_unregister(&maple_bus);
  768. cleanup:
  769. printk(KERN_INFO "Maple bus registration failed\n");
  770. return retval;
  771. }
  772. /* Push init to later to ensure hardware gets detected */
  773. fs_initcall(maple_bus_init);