| Stefan Richter | 8791833 | 2009-11-08 18:30:54 -0300 | [diff] [blame] | 1 | /* | 
 | 2 |  * FireDTV driver -- firewire I/O backend | 
 | 3 |  */ | 
 | 4 |  | 
 | 5 | #include <linux/device.h> | 
 | 6 | #include <linux/errno.h> | 
 | 7 | #include <linux/firewire.h> | 
 | 8 | #include <linux/firewire-constants.h> | 
| Stefan Richter | 8791833 | 2009-11-08 18:30:54 -0300 | [diff] [blame] | 9 | #include <linux/kernel.h> | 
 | 10 | #include <linux/list.h> | 
| Stefan Richter | a8aeb78 | 2009-11-18 16:00:55 -0300 | [diff] [blame] | 11 | #include <linux/mm.h> | 
| Stefan Richter | 8791833 | 2009-11-08 18:30:54 -0300 | [diff] [blame] | 12 | #include <linux/slab.h> | 
 | 13 | #include <linux/spinlock.h> | 
 | 14 | #include <linux/types.h> | 
 | 15 |  | 
 | 16 | #include <asm/page.h> | 
 | 17 |  | 
 | 18 | #include <dvb_demux.h> | 
 | 19 |  | 
 | 20 | #include "firedtv.h" | 
 | 21 |  | 
 | 22 | static LIST_HEAD(node_list); | 
 | 23 | static DEFINE_SPINLOCK(node_list_lock); | 
 | 24 |  | 
 | 25 | static inline struct fw_device *device_of(struct firedtv *fdtv) | 
 | 26 | { | 
 | 27 | 	return fw_device(fdtv->device->parent); | 
 | 28 | } | 
 | 29 |  | 
 | 30 | static int node_req(struct firedtv *fdtv, u64 addr, void *data, size_t len, | 
 | 31 | 		    int tcode) | 
 | 32 | { | 
 | 33 | 	struct fw_device *device = device_of(fdtv); | 
 | 34 | 	int rcode, generation = device->generation; | 
 | 35 |  | 
 | 36 | 	smp_rmb(); /* node_id vs. generation */ | 
 | 37 |  | 
 | 38 | 	rcode = fw_run_transaction(device->card, tcode, device->node_id, | 
 | 39 | 			generation, device->max_speed, addr, data, len); | 
 | 40 |  | 
 | 41 | 	return rcode != RCODE_COMPLETE ? -EIO : 0; | 
 | 42 | } | 
 | 43 |  | 
| Stefan Richter | 3fb80ef | 2009-11-18 16:02:01 -0300 | [diff] [blame] | 44 | static int node_lock(struct firedtv *fdtv, u64 addr, void *data) | 
| Stefan Richter | 8791833 | 2009-11-08 18:30:54 -0300 | [diff] [blame] | 45 | { | 
 | 46 | 	return node_req(fdtv, addr, data, 8, TCODE_LOCK_COMPARE_SWAP); | 
 | 47 | } | 
 | 48 |  | 
| Stefan Richter | 5375659 | 2009-11-18 16:01:34 -0300 | [diff] [blame] | 49 | static int node_read(struct firedtv *fdtv, u64 addr, void *data) | 
| Stefan Richter | 8791833 | 2009-11-08 18:30:54 -0300 | [diff] [blame] | 50 | { | 
| Stefan Richter | 5375659 | 2009-11-18 16:01:34 -0300 | [diff] [blame] | 51 | 	return node_req(fdtv, addr, data, 4, TCODE_READ_QUADLET_REQUEST); | 
| Stefan Richter | 8791833 | 2009-11-08 18:30:54 -0300 | [diff] [blame] | 52 | } | 
 | 53 |  | 
 | 54 | static int node_write(struct firedtv *fdtv, u64 addr, void *data, size_t len) | 
 | 55 | { | 
 | 56 | 	return node_req(fdtv, addr, data, len, TCODE_WRITE_BLOCK_REQUEST); | 
 | 57 | } | 
 | 58 |  | 
 | 59 | #define ISO_HEADER_SIZE			4 | 
 | 60 | #define CIP_HEADER_SIZE			8 | 
 | 61 | #define MPEG2_TS_HEADER_SIZE		4 | 
 | 62 | #define MPEG2_TS_SOURCE_PACKET_SIZE	(4 + 188) | 
 | 63 |  | 
 | 64 | #define MAX_PACKET_SIZE		1024  /* 776, rounded up to 2^n */ | 
 | 65 | #define PACKETS_PER_PAGE	(PAGE_SIZE / MAX_PACKET_SIZE) | 
 | 66 | #define N_PACKETS		64    /* buffer size */ | 
 | 67 | #define N_PAGES			DIV_ROUND_UP(N_PACKETS, PACKETS_PER_PAGE) | 
 | 68 | #define IRQ_INTERVAL		16 | 
 | 69 |  | 
 | 70 | struct firedtv_receive_context { | 
 | 71 | 	struct fw_iso_context *context; | 
 | 72 | 	struct fw_iso_buffer buffer; | 
 | 73 | 	int interrupt_packet; | 
 | 74 | 	int current_packet; | 
| Stefan Richter | a8aeb78 | 2009-11-18 16:00:55 -0300 | [diff] [blame] | 75 | 	char *pages[N_PAGES]; | 
| Stefan Richter | 8791833 | 2009-11-08 18:30:54 -0300 | [diff] [blame] | 76 | }; | 
 | 77 |  | 
 | 78 | static int queue_iso(struct firedtv_receive_context *ctx, int index) | 
 | 79 | { | 
 | 80 | 	struct fw_iso_packet p; | 
| Stefan Richter | 8791833 | 2009-11-08 18:30:54 -0300 | [diff] [blame] | 81 |  | 
 | 82 | 	p.payload_length = MAX_PACKET_SIZE; | 
| Stefan Richter | b1d33f4 | 2009-11-18 16:01:14 -0300 | [diff] [blame] | 83 | 	p.interrupt = !(++ctx->interrupt_packet & (IRQ_INTERVAL - 1)); | 
| Stefan Richter | 8791833 | 2009-11-08 18:30:54 -0300 | [diff] [blame] | 84 | 	p.skip = 0; | 
 | 85 | 	p.header_length = ISO_HEADER_SIZE; | 
 | 86 |  | 
| Stefan Richter | b1d33f4 | 2009-11-18 16:01:14 -0300 | [diff] [blame] | 87 | 	return fw_iso_context_queue(ctx->context, &p, &ctx->buffer, | 
 | 88 | 				    index * MAX_PACKET_SIZE); | 
| Stefan Richter | 8791833 | 2009-11-08 18:30:54 -0300 | [diff] [blame] | 89 | } | 
 | 90 |  | 
 | 91 | static void handle_iso(struct fw_iso_context *context, u32 cycle, | 
 | 92 | 		       size_t header_length, void *header, void *data) | 
 | 93 | { | 
 | 94 | 	struct firedtv *fdtv = data; | 
 | 95 | 	struct firedtv_receive_context *ctx = fdtv->backend_data; | 
 | 96 | 	__be32 *h, *h_end; | 
| Stefan Richter | a8aeb78 | 2009-11-18 16:00:55 -0300 | [diff] [blame] | 97 | 	int length, err, i = ctx->current_packet; | 
| Stefan Richter | 8791833 | 2009-11-08 18:30:54 -0300 | [diff] [blame] | 98 | 	char *p, *p_end; | 
 | 99 |  | 
 | 100 | 	for (h = header, h_end = h + header_length / 4; h < h_end; h++) { | 
 | 101 | 		length = be32_to_cpup(h) >> 16; | 
 | 102 | 		if (unlikely(length > MAX_PACKET_SIZE)) { | 
 | 103 | 			dev_err(fdtv->device, "length = %d\n", length); | 
 | 104 | 			length = MAX_PACKET_SIZE; | 
 | 105 | 		} | 
 | 106 |  | 
| Stefan Richter | a8aeb78 | 2009-11-18 16:00:55 -0300 | [diff] [blame] | 107 | 		p = ctx->pages[i / PACKETS_PER_PAGE] | 
 | 108 | 				+ (i % PACKETS_PER_PAGE) * MAX_PACKET_SIZE; | 
| Stefan Richter | 8791833 | 2009-11-08 18:30:54 -0300 | [diff] [blame] | 109 | 		p_end = p + length; | 
 | 110 |  | 
 | 111 | 		for (p += CIP_HEADER_SIZE + MPEG2_TS_HEADER_SIZE; p < p_end; | 
 | 112 | 		     p += MPEG2_TS_SOURCE_PACKET_SIZE) | 
 | 113 | 			dvb_dmx_swfilter_packets(&fdtv->demux, p, 1); | 
 | 114 |  | 
 | 115 | 		err = queue_iso(ctx, i); | 
 | 116 | 		if (unlikely(err)) | 
 | 117 | 			dev_err(fdtv->device, "requeue failed\n"); | 
 | 118 |  | 
 | 119 | 		i = (i + 1) & (N_PACKETS - 1); | 
 | 120 | 	} | 
 | 121 | 	ctx->current_packet = i; | 
 | 122 | } | 
 | 123 |  | 
 | 124 | static int start_iso(struct firedtv *fdtv) | 
 | 125 | { | 
 | 126 | 	struct firedtv_receive_context *ctx; | 
 | 127 | 	struct fw_device *device = device_of(fdtv); | 
| Stefan Richter | a8aeb78 | 2009-11-18 16:00:55 -0300 | [diff] [blame] | 128 | 	int i, err; | 
| Stefan Richter | 8791833 | 2009-11-08 18:30:54 -0300 | [diff] [blame] | 129 |  | 
 | 130 | 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); | 
 | 131 | 	if (!ctx) | 
 | 132 | 		return -ENOMEM; | 
 | 133 |  | 
 | 134 | 	ctx->context = fw_iso_context_create(device->card, | 
 | 135 | 			FW_ISO_CONTEXT_RECEIVE, fdtv->isochannel, | 
 | 136 | 			device->max_speed, ISO_HEADER_SIZE, handle_iso, fdtv); | 
 | 137 | 	if (IS_ERR(ctx->context)) { | 
 | 138 | 		err = PTR_ERR(ctx->context); | 
 | 139 | 		goto fail_free; | 
 | 140 | 	} | 
 | 141 |  | 
 | 142 | 	err = fw_iso_buffer_init(&ctx->buffer, device->card, | 
 | 143 | 				 N_PAGES, DMA_FROM_DEVICE); | 
 | 144 | 	if (err) | 
 | 145 | 		goto fail_context_destroy; | 
 | 146 |  | 
| Stefan Richter | b1d33f4 | 2009-11-18 16:01:14 -0300 | [diff] [blame] | 147 | 	ctx->interrupt_packet = 0; | 
| Stefan Richter | 8791833 | 2009-11-08 18:30:54 -0300 | [diff] [blame] | 148 | 	ctx->current_packet = 0; | 
 | 149 |  | 
| Stefan Richter | a8aeb78 | 2009-11-18 16:00:55 -0300 | [diff] [blame] | 150 | 	for (i = 0; i < N_PAGES; i++) | 
 | 151 | 		ctx->pages[i] = page_address(ctx->buffer.pages[i]); | 
| Stefan Richter | 8791833 | 2009-11-08 18:30:54 -0300 | [diff] [blame] | 152 |  | 
 | 153 | 	for (i = 0; i < N_PACKETS; i++) { | 
 | 154 | 		err = queue_iso(ctx, i); | 
 | 155 | 		if (err) | 
 | 156 | 			goto fail; | 
 | 157 | 	} | 
 | 158 |  | 
 | 159 | 	err = fw_iso_context_start(ctx->context, -1, 0, | 
 | 160 | 				   FW_ISO_CONTEXT_MATCH_ALL_TAGS); | 
 | 161 | 	if (err) | 
 | 162 | 		goto fail; | 
 | 163 |  | 
 | 164 | 	fdtv->backend_data = ctx; | 
 | 165 |  | 
 | 166 | 	return 0; | 
 | 167 | fail: | 
 | 168 | 	fw_iso_buffer_destroy(&ctx->buffer, device->card); | 
 | 169 | fail_context_destroy: | 
 | 170 | 	fw_iso_context_destroy(ctx->context); | 
 | 171 | fail_free: | 
 | 172 | 	kfree(ctx); | 
 | 173 |  | 
 | 174 | 	return err; | 
 | 175 | } | 
 | 176 |  | 
 | 177 | static void stop_iso(struct firedtv *fdtv) | 
 | 178 | { | 
 | 179 | 	struct firedtv_receive_context *ctx = fdtv->backend_data; | 
 | 180 |  | 
 | 181 | 	fw_iso_context_stop(ctx->context); | 
 | 182 | 	fw_iso_buffer_destroy(&ctx->buffer, device_of(fdtv)->card); | 
 | 183 | 	fw_iso_context_destroy(ctx->context); | 
 | 184 | 	kfree(ctx); | 
 | 185 | } | 
 | 186 |  | 
 | 187 | static const struct firedtv_backend backend = { | 
 | 188 | 	.lock		= node_lock, | 
 | 189 | 	.read		= node_read, | 
 | 190 | 	.write		= node_write, | 
 | 191 | 	.start_iso	= start_iso, | 
 | 192 | 	.stop_iso	= stop_iso, | 
 | 193 | }; | 
 | 194 |  | 
 | 195 | static void handle_fcp(struct fw_card *card, struct fw_request *request, | 
 | 196 | 		       int tcode, int destination, int source, int generation, | 
| Stefan Richter | 33e553f | 2010-06-20 22:50:35 +0200 | [diff] [blame] | 197 | 		       unsigned long long offset, void *payload, size_t length, | 
 | 198 | 		       void *callback_data) | 
| Stefan Richter | 8791833 | 2009-11-08 18:30:54 -0300 | [diff] [blame] | 199 | { | 
 | 200 | 	struct firedtv *f, *fdtv = NULL; | 
 | 201 | 	struct fw_device *device; | 
 | 202 | 	unsigned long flags; | 
 | 203 | 	int su; | 
 | 204 |  | 
| Clemens Ladisch | db5d247 | 2009-12-24 12:05:58 +0100 | [diff] [blame] | 205 | 	if (length < 2 || (((u8 *)payload)[0] & 0xf0) != 0) | 
| Stefan Richter | 8791833 | 2009-11-08 18:30:54 -0300 | [diff] [blame] | 206 | 		return; | 
| Stefan Richter | 8791833 | 2009-11-08 18:30:54 -0300 | [diff] [blame] | 207 |  | 
 | 208 | 	su = ((u8 *)payload)[1] & 0x7; | 
 | 209 |  | 
 | 210 | 	spin_lock_irqsave(&node_list_lock, flags); | 
 | 211 | 	list_for_each_entry(f, &node_list, list) { | 
 | 212 | 		device = device_of(f); | 
 | 213 | 		if (device->generation != generation) | 
 | 214 | 			continue; | 
 | 215 |  | 
 | 216 | 		smp_rmb(); /* node_id vs. generation */ | 
 | 217 |  | 
 | 218 | 		if (device->card == card && | 
 | 219 | 		    device->node_id == source && | 
 | 220 | 		    (f->subunit == su || (f->subunit == 0 && su == 0x7))) { | 
 | 221 | 			fdtv = f; | 
 | 222 | 			break; | 
 | 223 | 		} | 
 | 224 | 	} | 
 | 225 | 	spin_unlock_irqrestore(&node_list_lock, flags); | 
 | 226 |  | 
| Clemens Ladisch | db5d247 | 2009-12-24 12:05:58 +0100 | [diff] [blame] | 227 | 	if (fdtv) | 
| Stefan Richter | 8791833 | 2009-11-08 18:30:54 -0300 | [diff] [blame] | 228 | 		avc_recv(fdtv, payload, length); | 
| Stefan Richter | 8791833 | 2009-11-08 18:30:54 -0300 | [diff] [blame] | 229 | } | 
 | 230 |  | 
 | 231 | static struct fw_address_handler fcp_handler = { | 
 | 232 | 	.length           = CSR_FCP_END - CSR_FCP_RESPONSE, | 
 | 233 | 	.address_callback = handle_fcp, | 
 | 234 | }; | 
 | 235 |  | 
 | 236 | static const struct fw_address_region fcp_region = { | 
 | 237 | 	.start	= CSR_REGISTER_BASE + CSR_FCP_RESPONSE, | 
 | 238 | 	.end	= CSR_REGISTER_BASE + CSR_FCP_END, | 
 | 239 | }; | 
 | 240 |  | 
 | 241 | /* Adjust the template string if models with longer names appear. */ | 
| Clemens Ladisch | 1f8fef7 | 2009-12-24 11:59:57 +0100 | [diff] [blame] | 242 | #define MAX_MODEL_NAME_LEN sizeof("FireDTV ????") | 
| Stefan Richter | 8791833 | 2009-11-08 18:30:54 -0300 | [diff] [blame] | 243 |  | 
 | 244 | static int node_probe(struct device *dev) | 
 | 245 | { | 
 | 246 | 	struct firedtv *fdtv; | 
| Clemens Ladisch | 1f8fef7 | 2009-12-24 11:59:57 +0100 | [diff] [blame] | 247 | 	char name[MAX_MODEL_NAME_LEN]; | 
| Stefan Richter | 8791833 | 2009-11-08 18:30:54 -0300 | [diff] [blame] | 248 | 	int name_len, err; | 
 | 249 |  | 
| Clemens Ladisch | 1f8fef7 | 2009-12-24 11:59:57 +0100 | [diff] [blame] | 250 | 	name_len = fw_csr_string(fw_unit(dev)->directory, CSR_MODEL, | 
 | 251 | 				 name, sizeof(name)); | 
| Stefan Richter | 8791833 | 2009-11-08 18:30:54 -0300 | [diff] [blame] | 252 |  | 
| Clemens Ladisch | 1f8fef7 | 2009-12-24 11:59:57 +0100 | [diff] [blame] | 253 | 	fdtv = fdtv_alloc(dev, &backend, name, name_len >= 0 ? name_len : 0); | 
| Stefan Richter | 8791833 | 2009-11-08 18:30:54 -0300 | [diff] [blame] | 254 | 	if (!fdtv) | 
 | 255 | 		return -ENOMEM; | 
 | 256 |  | 
 | 257 | 	err = fdtv_register_rc(fdtv, dev); | 
 | 258 | 	if (err) | 
 | 259 | 		goto fail_free; | 
 | 260 |  | 
 | 261 | 	spin_lock_irq(&node_list_lock); | 
 | 262 | 	list_add_tail(&fdtv->list, &node_list); | 
 | 263 | 	spin_unlock_irq(&node_list_lock); | 
 | 264 |  | 
 | 265 | 	err = avc_identify_subunit(fdtv); | 
 | 266 | 	if (err) | 
 | 267 | 		goto fail; | 
 | 268 |  | 
 | 269 | 	err = fdtv_dvb_register(fdtv); | 
 | 270 | 	if (err) | 
 | 271 | 		goto fail; | 
 | 272 |  | 
 | 273 | 	avc_register_remote_control(fdtv); | 
 | 274 |  | 
 | 275 | 	return 0; | 
 | 276 | fail: | 
 | 277 | 	spin_lock_irq(&node_list_lock); | 
 | 278 | 	list_del(&fdtv->list); | 
 | 279 | 	spin_unlock_irq(&node_list_lock); | 
 | 280 | 	fdtv_unregister_rc(fdtv); | 
 | 281 | fail_free: | 
 | 282 | 	kfree(fdtv); | 
 | 283 |  | 
 | 284 | 	return err; | 
 | 285 | } | 
 | 286 |  | 
 | 287 | static int node_remove(struct device *dev) | 
 | 288 | { | 
 | 289 | 	struct firedtv *fdtv = dev_get_drvdata(dev); | 
 | 290 |  | 
 | 291 | 	fdtv_dvb_unregister(fdtv); | 
 | 292 |  | 
 | 293 | 	spin_lock_irq(&node_list_lock); | 
 | 294 | 	list_del(&fdtv->list); | 
 | 295 | 	spin_unlock_irq(&node_list_lock); | 
 | 296 |  | 
 | 297 | 	fdtv_unregister_rc(fdtv); | 
 | 298 |  | 
 | 299 | 	kfree(fdtv); | 
 | 300 | 	return 0; | 
 | 301 | } | 
 | 302 |  | 
 | 303 | static void node_update(struct fw_unit *unit) | 
 | 304 | { | 
 | 305 | 	struct firedtv *fdtv = dev_get_drvdata(&unit->device); | 
 | 306 |  | 
 | 307 | 	if (fdtv->isochannel >= 0) | 
 | 308 | 		cmp_establish_pp_connection(fdtv, fdtv->subunit, | 
 | 309 | 					    fdtv->isochannel); | 
 | 310 | } | 
 | 311 |  | 
 | 312 | static struct fw_driver fdtv_driver = { | 
 | 313 | 	.driver   = { | 
 | 314 | 		.owner  = THIS_MODULE, | 
 | 315 | 		.name   = "firedtv", | 
 | 316 | 		.bus    = &fw_bus_type, | 
 | 317 | 		.probe  = node_probe, | 
 | 318 | 		.remove = node_remove, | 
 | 319 | 	}, | 
 | 320 | 	.update   = node_update, | 
 | 321 | 	.id_table = fdtv_id_table, | 
 | 322 | }; | 
 | 323 |  | 
 | 324 | int __init fdtv_fw_init(void) | 
 | 325 | { | 
 | 326 | 	int ret; | 
 | 327 |  | 
 | 328 | 	ret = fw_core_add_address_handler(&fcp_handler, &fcp_region); | 
 | 329 | 	if (ret < 0) | 
 | 330 | 		return ret; | 
 | 331 |  | 
 | 332 | 	return driver_register(&fdtv_driver.driver); | 
 | 333 | } | 
 | 334 |  | 
 | 335 | void fdtv_fw_exit(void) | 
 | 336 | { | 
 | 337 | 	driver_unregister(&fdtv_driver.driver); | 
 | 338 | 	fw_core_remove_address_handler(&fcp_handler); | 
 | 339 | } |