Skip to contentSkip to conversation

Maple bus support for the Sega Dreamcast

PATCH In review Adrian McMenamin 2 participants 4 messages Sep 9, 2007

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 Maple bus support for the Sega Dreamcast

This patch adds support for Sega's proprietary Maple bus - which is required to support the Dreamcast's peripherals.

This driver represents a substantial re-write of the old 2.4 driver.

Signed-off by: Adrian McMenamin <[email protected]>

arch/sh/Kconfig +11 -0
index 54878f0..c1771b7 100644
@@ -702,6 +702,17 @@ config CF_BASE_ADDR
 	default "0xb8000000" if CF_AREA6
 	default "0xb4000000" if CF_AREA5
 
+config MAPLE
+	tristate "Maple Bus Support"
+	depends on SH_DREAMCAST
+	help
+	  The Maple Bus is SEGA's serial communication bus for peripherals
+	  on the Dreamcast. Without this bus support you won't be able to
+	  get your Dreamcast keyboard etc to work, so most users
+	  probably want to say 'Y' here, unless you are only using the
+	  Dreamcast with a serial line terminal or a remote network
+	  connection.
+
 source "arch/sh/drivers/pci/Kconfig"
 
 source "drivers/pci/Kconfig"
drivers/sh/Makefile +1 -0
index 8a14389..f0a1f4f 100644
@@ -3,4 +3,5 @@
 #
 
 obj-$(CONFIG_SUPERHYWAY) += superhyway/
+obj-$(CONFIG_MAPLE) += maple/
 
drivers/sh/maple/Makefile new file +3 -0
new file mode 100644
index 0000000..f8c39f2
@@ -0,0 +1,3 @@
+#Makefile for Maple Bus
+
+obj-y	:= maplebus.o
drivers/sh/maple/maplebus.c new file +737 -0

737 lines not on this page · raw message

include/linux/input.h +0 -0
include/linux/maple.h new file +17 -0
new file mode 100644
index 0000000..1bb565d
@@ -0,0 +1,126 @@
+/**
+ * maple.h
+ *
+ * porting to 2.6 driver model
+ * copyright Adrian McMenamin, 2007
+ *
+ */
+
+extern struct bus_type maple_bus_type;
+extern struct semaphore maple_mutex;
+
+#define MAPLE_PORTS 4
+#define MAPLE_PNP_INTERVAL HZ
+#define MAPLE_MAXPACKETS 8
+#define MAPLE_DMA_ORDER 14
+#define MAPLE_DMA_SIZE (1 << MAPLE_DMA_ORDER)
+#define MAPLE_DMA_PAGES ((MAPLE_DMA_ORDER > PAGE_SHIFT) ?

MAPLE_DMA_ORDER - PAGE_SHIFT : 0)
+
+/* Maple Bus registers */
+#define MAPLE_BASE 0xa05f6c00
+#define MAPLE_DMAADDR (MAPLE_BASE+0x04)
+#define MAPLE_TRIGTYPE (MAPLE_BASE+0x10)
+#define MAPLE_ENABLE (MAPLE_BASE+0x14)
+#define MAPLE_STATE (MAPLE_BASE+0x18)
+#define MAPLE_SPEED (MAPLE_BASE+0x80)
+#define MAPLE_RESET (MAPLE_BASE+0x8c)
+
+#define MAPLE_MAGIC 0x6155404f
+#define MAPLE_2MBPS 0
+#define MAPLE_TIMEOUT(n) ((n)<<15)
+
+/* Maple Bus command and response codes */
+enum maple_code {
+ MAPLE_RESPONSE_FILEERR = -5,
+ MAPLE_RESPONSE_AGAIN = -4, /* request should be retransmitted */
+ MAPLE_RESPONSE_BADCMD = -3,
+ MAPLE_RESPONSE_BADFUNC = -2,
+ MAPLE_RESPONSE_NONE = -1, /* unit didn't respond at all */
+ MAPLE_COMMAND_DEVINFO = 1,
+ MAPLE_COMMAND_ALLINFO = 2,
+ MAPLE_COMMAND_RESET = 3,
+ MAPLE_COMMAND_KILL = 4,
+ MAPLE_RESPONSE_DEVINFO = 5,
+ MAPLE_RESPONSE_ALLINFO = 6,
+ MAPLE_RESPONSE_OK = 7,
+ MAPLE_RESPONSE_DATATRF = 8,
+ MAPLE_COMMAND_GETCOND = 9,
+ MAPLE_COMMAND_GETMINFO = 10,
+ MAPLE_COMMAND_BREAD = 11,
+ MAPLE_COMMAND_BWRITE = 12,
+ MAPLE_COMMAND_SETCOND = 14
+};
+
+/* Function codes */
+
+#define MAPLE_FUNC_CONTROLLER 0x001

+#define MAPLE_FUNC_MEMCARD    0x002
+#define MAPLE_FUNC_LCD        0x004
+#define MAPLE_FUNC_CLOCK      0x008
+#define MAPLE_FUNC_MICROPHONE 0x010
+#define MAPLE_FUNC_ARGUN      0x020
+#define MAPLE_FUNC_KEYBOARD   0x040
+#define MAPLE_FUNC_LIGHTGUN   0x080
+#define MAPLE_FUNC_PURUPURU   0x100
+#define MAPLE_FUNC_MOUSE      0x200

+
+struct mapleq {
+ struct list_head list;
+ struct maple_device *dev;
+ void *sendbuf, *recvbuf;
+ unsigned char length;
+ enum maple_code command;
+};
+
+struct maple_devinfo {
+ unsigned long function;
+ unsigned long function_data[3];
+ unsigned char area_code;
+ unsigned char connector_directon;
+ char product_name[31];
+ char product_licence[61];
+ unsigned short standby_power;
+ unsigned short max_power;
+};
+
+struct maple_device {
+ struct maple_driver *driver;
+ struct mapleq *mq;
+ void *private_data;
+ void (*callback) (struct mapleq * mq);
+ unsigned long when, interval, function;
+ struct maple_devinfo devinfo;
+ unsigned char port, unit;
+ char product_name[32];
+ char product_licence[64];
+ int registered;
+ struct device dev;
+};
+
+struct maple_driver {
+ unsigned long function;
+ int (*connect) (struct maple_device * dev);
+ void (*disconnect) (struct maple_device * dev);
+ struct device_driver drv;
+};
+
+struct device_specify {
+ int port;
+ int unit;
+};
+
+void maple_getcond_callback(struct maple_device *dev,

+			    void (*callback) (struct mapleq * mq),
+			    unsigned long interval,
+			    unsigned long function);

+
+void maple_setup_port_rescan(struct maple_device *mdev); +
+void maplebus_init_hardware(void);
+
+int maple_dma_done(void);
+
+int maple_driver_register(struct device_driver *drv);
+
+#define to_maple_dev(n) container_of(n, struct maple_device, dev) +#define to_maple_driver(n) container_of(n, struct maple_driver, drv)

#1 lore raw
Quoted 2 lines · On Sun, 9 Sep 2007 17:46:54 +0100 "Adrian McMenamin" <[email protected]>…

This patch adds support for Sega's proprietary Maple bus - which is required to support the Dreamcast's peripherals.

Hi,

in general the code looks clean; great job on that.
A few suggestions and comments to hopefully help this driver to become even better:

First of all, I'm a little concerned about the lack of locking that this driver seems to have; what guarantees the integrity of the lists used in the driver?

Second, you have a lot of use of likely() and unlikely(); so much that I think it's waaaay overkill. The code it's in generally isn't hotpath, and gcc also does a pretty decent job without giving these hints in general.

drivers/sh/maple/maplebus.c line 106

Quoted 5 lines
drivers/sh/maple/maplebus.c new file +5 -0
+
+void maple_add_packet(struct mapleq *mq)
+{
+	list_add((struct list_head *) mq, &maple_waitq);
+}

for example this list.. what makes sure that no 2 pieces of code muck with it at the same time?

drivers/sh/maple/maplebus.c line 138

Quoted 13 lines
drivers/sh/maple/maplebus.c new file +13 -0
+static struct maple_device *maple_alloc_dev(int port, int unit)
+{
+	struct maple_device *dev;
+
+	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
+	if (unlikely(!dev))
+		return NULL;
+
+	dev->port = port;
+	dev->unit = unit;
+	dev->mq = maple_allocq(dev);
+
+	if (unlikely(!dev->mq)) {

this unlikely isn't needed; gcc basically assumes that NULL pointer checks are unlikely anyway...

drivers/sh/maple/maplebus.c line 326

Quoted 7 lines
drivers/sh/maple/maplebus.c new file +7 -0
+
+static int setup_maple_commands(struct device *device, void *ignored)
+{
+	struct maple_device *maple_dev = to_maple_dev(device);
+
+	if (likely(maple_dev->interval > 0)) {
+		if (likely(jiffies > maple_dev->when)) {

this is I think a small bug; it is for sure unsafe against jiffies wrapping... please consider using the time_before() and time_after() helpers which will make this comparison safe wrt jiffies wrapping. The same is true for all other jiffies use in the driver...

drivers/sh/maple/maplebus.c line 511

Quoted 6 lines
drivers/sh/maple/maplebus.c new file +6 -0
+static irqreturn_t maplebus_dma_interrupt(int irq, void *dev_id)
+{
+	/* Load everything into the bottom half */
+	schedule_work(&maple_dma_process);
+	return IRQ_HANDLED;
+}

I wonder if you want to at least check if the work was really for you before returning IRQ_HANDLED....

#2 lore raw
Quoted 12 lines · Arjan van de Ven
Quoted 2 lines · On Sun, 9 Sep 2007 17:46:54 +0100 "Adrian McMenamin" <[email protected]>…

This patch adds support for Sega's proprietary Maple bus - which is required to support the Dreamcast's peripherals.

First of all, I'm a little concerned about the lack of locking that this driver seems to have; what guarantees the integrity of the lists used in the driver?

Could you explain what you think the issue is? The lists are only handled by this driver and not anything else.

Quoted 4 lines

Second, you have a lot of use of likely() and unlikely(); so much that I think it's waaaay overkill. The code it's in generally isn't hotpath, and gcc also does a pretty decent job without giving these hints in general.

Fair enough.

Quoted 9 lines

drivers/sh/maple/maplebus.c line 106

Quoted 5 lines
drivers/sh/maple/maplebus.c new file +5 -0
+
+void maple_add_packet(struct mapleq *mq)
+{
+     list_add((struct list_head *) mq, &maple_waitq);
+}

for example this list.. what makes sure that no 2 pieces of code muck with it at the same time?

Because everything is handled by one kernel thread on a uniprocessor machine.

Quoted 9 lines

drivers/sh/maple/maplebus.c line 511

Quoted 6 lines
drivers/sh/maple/maplebus.c new file +6 -0
+static irqreturn_t maplebus_dma_interrupt(int irq, void *dev_id)
+{
+     /* Load everything into the bottom half */
+     schedule_work(&maple_dma_process);
+     return IRQ_HANDLED;
+}

I wonder if you want to at least check if the work was really for you before returning IRQ_HANDLED....

It's *always* for me - this is an end of DMA interrupt for the bus.

#3 lore raw
Quoted 13 lines · On Sun, 9 Sep 2007 19:36:52 +0100 "Adrian McMenamin" <[email protected]>…

Fair enough.

Quoted 10 lines

drivers/sh/maple/maplebus.c line 106

Quoted 5 lines
drivers/sh/maple/maplebus.c new file +5 -0
+
+void maple_add_packet(struct mapleq *mq)
+{
+     list_add((struct list_head *) mq, &maple_waitq);
+}

for example this list.. what makes sure that no 2 pieces of code muck with it at the same time?

Quoted 2 lines

Because everything is handled by one kernel thread on a uniprocessor machine.

hmmm.... still. Once you look at it from any other place... preempt etc etc... it's sort of playing russian roulette with an empty gun, but without checking that the gun is actually empty... at some point someone may accidentally leave a bullet in ;)