firedtv-1394.c 6.7 KB
Newer Older
G
Greg Kroah-Hartman 已提交
1
/*
2
 * FireDTV driver -- ieee1394 I/O backend
G
Greg Kroah-Hartman 已提交
3
 *
4 5 6
 * Copyright (C) 2004 Andreas Monitzer <andy@monitzer.com>
 * Copyright (C) 2007-2008 Ben Backx <ben@bbackx.com>
 * Copyright (C) 2008 Henrik Kurelid <henrik@kurelid.se>
G
Greg Kroah-Hartman 已提交
7 8 9 10 11 12 13
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License as
 *	published by the Free Software Foundation; either version 2 of
 *	the License, or (at your option) any later version.
 */

14
#include <linux/device.h>
G
Greg Kroah-Hartman 已提交
15
#include <linux/errno.h>
16 17
#include <linux/kernel.h>
#include <linux/list.h>
18
#include <linux/slab.h>
19 20 21
#include <linux/spinlock.h>
#include <linux/types.h>

S
Stefan Richter 已提交
22
#include <dma.h>
23
#include <csr1212.h>
G
Greg Kroah-Hartman 已提交
24 25
#include <highlevel.h>
#include <hosts.h>
S
Stefan Richter 已提交
26 27
#include <ieee1394.h>
#include <iso.h>
28
#include <nodemgr.h>
G
Greg Kroah-Hartman 已提交
29

30 31
#include <dvb_demux.h>

32
#include "firedtv.h"
G
Greg Kroah-Hartman 已提交
33

S
Stefan Richter 已提交
34 35
static LIST_HEAD(node_list);
static DEFINE_SPINLOCK(node_list_lock);
G
Greg Kroah-Hartman 已提交
36

37 38 39
#define CIP_HEADER_SIZE			8
#define MPEG2_TS_HEADER_SIZE		4
#define MPEG2_TS_SOURCE_PACKET_SIZE	(4 + 188)
G
Greg Kroah-Hartman 已提交
40

S
Stefan Richter 已提交
41
static void rawiso_activity_cb(struct hpsb_iso *iso)
G
Greg Kroah-Hartman 已提交
42
{
S
Stefan Richter 已提交
43 44 45
	struct firedtv *f, *fdtv = NULL;
	unsigned int i, num, packet;
	unsigned char *buf;
G
Greg Kroah-Hartman 已提交
46
	unsigned long flags;
S
Stefan Richter 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60
	int count;

	spin_lock_irqsave(&node_list_lock, flags);
	list_for_each_entry(f, &node_list, list)
		if (f->backend_data == iso) {
			fdtv = f;
			break;
		}
	spin_unlock_irqrestore(&node_list_lock, flags);

	packet = iso->first_packet;
	num = hpsb_iso_n_ready(iso);

	if (!fdtv) {
61
		pr_err("received at unknown iso channel\n");
S
Stefan Richter 已提交
62 63
		goto out;
	}
G
Greg Kroah-Hartman 已提交
64

S
Stefan Richter 已提交
65 66 67 68
	for (i = 0; i < num; i++, packet = (packet + 1) % iso->buf_packets) {
		buf = dma_region_i(&iso->data_buf, unsigned char,
			iso->infos[packet].offset + CIP_HEADER_SIZE);
		count = (iso->infos[packet].len - CIP_HEADER_SIZE) /
69
			MPEG2_TS_SOURCE_PACKET_SIZE;
S
Stefan Richter 已提交
70 71 72 73 74 75

		/* ignore empty packet */
		if (iso->infos[packet].len <= CIP_HEADER_SIZE)
			continue;

		while (count--) {
76
			if (buf[MPEG2_TS_HEADER_SIZE] == 0x47)
S
Stefan Richter 已提交
77
				dvb_dmx_swfilter_packets(&fdtv->demux,
78
						&buf[MPEG2_TS_HEADER_SIZE], 1);
S
Stefan Richter 已提交
79 80 81
			else
				dev_err(fdtv->device,
					"skipping invalid packet\n");
82
			buf += MPEG2_TS_SOURCE_PACKET_SIZE;
G
Greg Kroah-Hartman 已提交
83
		}
S
Stefan Richter 已提交
84 85 86 87 88 89 90 91 92 93
	}
out:
	hpsb_iso_recv_release_packets(iso, num);
}

static inline struct node_entry *node_of(struct firedtv *fdtv)
{
	return container_of(fdtv->device, struct unit_directory, device)->ne;
}

94
static int node_lock(struct firedtv *fdtv, u64 addr, void *data)
S
Stefan Richter 已提交
95
{
96
	quadlet_t *d = data;
97 98
	int ret;

99 100 101
	ret = hpsb_node_lock(node_of(fdtv), addr,
			     EXTCODE_COMPARE_SWAP, &d[1], d[0]);
	d[0] = d[1];
102 103

	return ret;
S
Stefan Richter 已提交
104 105
}

106
static int node_read(struct firedtv *fdtv, u64 addr, void *data)
S
Stefan Richter 已提交
107
{
108
	return hpsb_node_read(node_of(fdtv), addr, data, 4);
S
Stefan Richter 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
}

static int node_write(struct firedtv *fdtv, u64 addr, void *data, size_t len)
{
	return hpsb_node_write(node_of(fdtv), addr, data, len);
}

#define FDTV_ISO_BUFFER_PACKETS 256
#define FDTV_ISO_BUFFER_SIZE (FDTV_ISO_BUFFER_PACKETS * 200)

static int start_iso(struct firedtv *fdtv)
{
	struct hpsb_iso *iso_handle;
	int ret;

	iso_handle = hpsb_iso_recv_init(node_of(fdtv)->host,
				FDTV_ISO_BUFFER_SIZE, FDTV_ISO_BUFFER_PACKETS,
				fdtv->isochannel, HPSB_ISO_DMA_DEFAULT,
				-1, /* stat.config.irq_interval */
				rawiso_activity_cb);
	if (iso_handle == NULL) {
		dev_err(fdtv->device, "cannot initialize iso receive\n");
		return -ENOMEM;
	}
	fdtv->backend_data = iso_handle;

	ret = hpsb_iso_recv_start(iso_handle, -1, -1, 0);
	if (ret != 0) {
		dev_err(fdtv->device, "cannot start iso receive\n");
		hpsb_iso_shutdown(iso_handle);
		fdtv->backend_data = NULL;
	}
	return ret;
}

static void stop_iso(struct firedtv *fdtv)
{
	struct hpsb_iso *iso_handle = fdtv->backend_data;
G
Greg Kroah-Hartman 已提交
147

S
Stefan Richter 已提交
148 149 150
	if (iso_handle != NULL) {
		hpsb_iso_stop(iso_handle);
		hpsb_iso_shutdown(iso_handle);
151
	}
S
Stefan Richter 已提交
152
	fdtv->backend_data = NULL;
G
Greg Kroah-Hartman 已提交
153 154
}

S
Stefan Richter 已提交
155 156 157 158 159 160
static const struct firedtv_backend fdtv_1394_backend = {
	.lock		= node_lock,
	.read		= node_read,
	.write		= node_write,
	.start_iso	= start_iso,
	.stop_iso	= stop_iso,
161 162
};

S
Stefan Richter 已提交
163 164
static void fcp_request(struct hpsb_host *host, int nodeid, int direction,
			int cts, u8 *data, size_t length)
G
Greg Kroah-Hartman 已提交
165
{
S
Stefan Richter 已提交
166
	struct firedtv *f, *fdtv = NULL;
G
Greg Kroah-Hartman 已提交
167
	unsigned long flags;
S
Stefan Richter 已提交
168
	int su;
G
Greg Kroah-Hartman 已提交
169

S
Stefan Richter 已提交
170 171
	if (length == 0 || (data[0] & 0xf0) != 0)
		return;
172

S
Stefan Richter 已提交
173 174 175 176 177 178 179 180 181 182 183
	su = data[1] & 0x7;

	spin_lock_irqsave(&node_list_lock, flags);
	list_for_each_entry(f, &node_list, list)
		if (node_of(f)->host == host &&
		    node_of(f)->nodeid == nodeid &&
		    (f->subunit == su || (f->subunit == 0 && su == 0x7))) {
			fdtv = f;
			break;
		}
	spin_unlock_irqrestore(&node_list_lock, flags);
184

S
Stefan Richter 已提交
185 186 187 188 189 190 191 192 193 194 195
	if (fdtv)
		avc_recv(fdtv, data, length);
}

static int node_probe(struct device *dev)
{
	struct unit_directory *ud =
			container_of(dev, struct unit_directory, device);
	struct firedtv *fdtv;
	int kv_len, err;
	void *kv_str;
196

197 198 199 200 201 202 203
	if (ud->model_name_kv) {
		kv_len = (ud->model_name_kv->value.leaf.len - 2) * 4;
		kv_str = CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(ud->model_name_kv);
	} else {
		kv_len = 0;
		kv_str = NULL;
	}
S
Stefan Richter 已提交
204 205 206
	fdtv = fdtv_alloc(dev, &fdtv_1394_backend, kv_str, kv_len);
	if (!fdtv)
		return -ENOMEM;
207 208 209 210 211

	/*
	 * Work around a bug in udev's path_id script:  Use the fw-host's dev
	 * instead of the unit directory's dev as parent of the input device.
	 */
212
	err = fdtv_register_rc(fdtv, dev->parent->parent);
213 214 215
	if (err)
		goto fail_free;

S
Stefan Richter 已提交
216 217 218
	spin_lock_irq(&node_list_lock);
	list_add_tail(&fdtv->list, &node_list);
	spin_unlock_irq(&node_list_lock);
219

220
	err = avc_identify_subunit(fdtv);
221 222
	if (err)
		goto fail;
G
Greg Kroah-Hartman 已提交
223

S
Stefan Richter 已提交
224
	err = fdtv_dvb_register(fdtv);
225 226
	if (err)
		goto fail;
G
Greg Kroah-Hartman 已提交
227

228
	avc_register_remote_control(fdtv);
229

230 231
	return 0;
fail:
S
Stefan Richter 已提交
232
	spin_lock_irq(&node_list_lock);
233
	list_del(&fdtv->list);
S
Stefan Richter 已提交
234
	spin_unlock_irq(&node_list_lock);
235
	fdtv_unregister_rc(fdtv);
236
fail_free:
237
	kfree(fdtv);
238

239
	return err;
G
Greg Kroah-Hartman 已提交
240 241
}

S
Stefan Richter 已提交
242
static int node_remove(struct device *dev)
G
Greg Kroah-Hartman 已提交
243
{
244
	struct firedtv *fdtv = dev_get_drvdata(dev);
G
Greg Kroah-Hartman 已提交
245

S
Stefan Richter 已提交
246 247 248
	fdtv_dvb_unregister(fdtv);

	spin_lock_irq(&node_list_lock);
249
	list_del(&fdtv->list);
S
Stefan Richter 已提交
250
	spin_unlock_irq(&node_list_lock);
G
Greg Kroah-Hartman 已提交
251

252 253
	fdtv_unregister_rc(fdtv);
	kfree(fdtv);
254

G
Greg Kroah-Hartman 已提交
255 256 257
	return 0;
}

S
Stefan Richter 已提交
258
static int node_update(struct unit_directory *ud)
G
Greg Kroah-Hartman 已提交
259
{
260
	struct firedtv *fdtv = dev_get_drvdata(&ud->device);
G
Greg Kroah-Hartman 已提交
261

262 263 264
	if (fdtv->isochannel >= 0)
		cmp_establish_pp_connection(fdtv, fdtv->subunit,
					    fdtv->isochannel);
G
Greg Kroah-Hartman 已提交
265 266 267
	return 0;
}

268
static struct hpsb_protocol_driver fdtv_driver = {
269
	.name		= "firedtv",
270
	.id_table	= fdtv_id_table,
S
Stefan Richter 已提交
271
	.update		= node_update,
G
Greg Kroah-Hartman 已提交
272
	.driver         = {
S
Stefan Richter 已提交
273 274
		.probe  = node_probe,
		.remove = node_remove,
G
Greg Kroah-Hartman 已提交
275 276 277
	},
};

278
static struct hpsb_highlevel fdtv_highlevel = {
279 280 281 282
	.name		= "firedtv",
	.fcp_request	= fcp_request,
};

283
int __init fdtv_1394_init(void)
G
Greg Kroah-Hartman 已提交
284 285 286
{
	int ret;

287 288
	hpsb_register_highlevel(&fdtv_highlevel);
	ret = hpsb_register_protocol(&fdtv_driver);
G
Greg Kroah-Hartman 已提交
289
	if (ret) {
290
		printk(KERN_ERR "firedtv: failed to register protocol\n");
291
		hpsb_unregister_highlevel(&fdtv_highlevel);
292 293
	}
	return ret;
G
Greg Kroah-Hartman 已提交
294 295
}

S
Stefan Richter 已提交
296
void __exit fdtv_1394_exit(void)
G
Greg Kroah-Hartman 已提交
297
{
298 299
	hpsb_unregister_protocol(&fdtv_driver);
	hpsb_unregister_highlevel(&fdtv_highlevel);
G
Greg Kroah-Hartman 已提交
300
}