blob: 22ea4c90f5c9221c0113ac0b001bdae46e86fdd7 [file] [log] [blame]
Rambaldia70f81c2009-01-17 14:47:34 +01001/*
2 * FireDTV driver (formerly known as FireSAT)
3 *
4 * Copyright (C) 2004 Andreas Monitzer <andy@monitzer.com>
5 * Copyright (C) 2007-2008 Ben Backx <ben@bbackx.com>
6 * Copyright (C) 2008 Henrik Kurelid <henrik@kurelid.se>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 */
13
14#include <linux/device.h>
15#include <linux/errno.h>
16#include <linux/kernel.h>
17#include <linux/list.h>
Rambaldia70f81c2009-01-17 14:47:34 +010018#include <linux/spinlock.h>
Rambaldia70f81c2009-01-17 14:47:34 +010019#include <linux/types.h>
20
Stefan Richter15490792009-02-23 14:21:10 +010021#include <dma.h>
Rambaldia70f81c2009-01-17 14:47:34 +010022#include <csr1212.h>
23#include <highlevel.h>
24#include <hosts.h>
Stefan Richter15490792009-02-23 14:21:10 +010025#include <ieee1394.h>
26#include <iso.h>
Rambaldia70f81c2009-01-17 14:47:34 +010027#include <nodemgr.h>
28
Rambaldia70f81c2009-01-17 14:47:34 +010029#include "firedtv.h"
Rambaldia70f81c2009-01-17 14:47:34 +010030
Stefan Richter15490792009-02-23 14:21:10 +010031static LIST_HEAD(node_list);
32static DEFINE_SPINLOCK(node_list_lock);
Rambaldia70f81c2009-01-17 14:47:34 +010033
Stefan Richter15490792009-02-23 14:21:10 +010034#define FIREWIRE_HEADER_SIZE 4
35#define CIP_HEADER_SIZE 8
Rambaldia70f81c2009-01-17 14:47:34 +010036
Stefan Richter15490792009-02-23 14:21:10 +010037static void rawiso_activity_cb(struct hpsb_iso *iso)
Rambaldia70f81c2009-01-17 14:47:34 +010038{
Stefan Richter15490792009-02-23 14:21:10 +010039 struct firedtv *f, *fdtv = NULL;
40 unsigned int i, num, packet;
41 unsigned char *buf;
Rambaldia70f81c2009-01-17 14:47:34 +010042 unsigned long flags;
Stefan Richter15490792009-02-23 14:21:10 +010043 int count;
Rambaldia70f81c2009-01-17 14:47:34 +010044
Stefan Richter15490792009-02-23 14:21:10 +010045 spin_lock_irqsave(&node_list_lock, flags);
46 list_for_each_entry(f, &node_list, list)
47 if (f->backend_data == iso) {
48 fdtv = f;
49 break;
Rambaldia70f81c2009-01-17 14:47:34 +010050 }
Stefan Richter15490792009-02-23 14:21:10 +010051 spin_unlock_irqrestore(&node_list_lock, flags);
Rambaldia70f81c2009-01-17 14:47:34 +010052
Stefan Richter15490792009-02-23 14:21:10 +010053 packet = iso->first_packet;
54 num = hpsb_iso_n_ready(iso);
55
56 if (!fdtv) {
57 dev_err(fdtv->device, "received at unknown iso channel\n");
58 goto out;
Rambaldia70f81c2009-01-17 14:47:34 +010059 }
Stefan Richter15490792009-02-23 14:21:10 +010060
61 for (i = 0; i < num; i++, packet = (packet + 1) % iso->buf_packets) {
62 buf = dma_region_i(&iso->data_buf, unsigned char,
63 iso->infos[packet].offset + CIP_HEADER_SIZE);
64 count = (iso->infos[packet].len - CIP_HEADER_SIZE) /
65 (188 + FIREWIRE_HEADER_SIZE);
66
67 /* ignore empty packet */
68 if (iso->infos[packet].len <= CIP_HEADER_SIZE)
69 continue;
70
71 while (count--) {
72 if (buf[FIREWIRE_HEADER_SIZE] == 0x47)
73 dvb_dmx_swfilter_packets(&fdtv->demux,
74 &buf[FIREWIRE_HEADER_SIZE], 1);
75 else
76 dev_err(fdtv->device,
77 "skipping invalid packet\n");
78 buf += 188 + FIREWIRE_HEADER_SIZE;
79 }
80 }
81out:
82 hpsb_iso_recv_release_packets(iso, num);
Rambaldia70f81c2009-01-17 14:47:34 +010083}
84
Stefan Richter15490792009-02-23 14:21:10 +010085static inline struct node_entry *node_of(struct firedtv *fdtv)
86{
87 return container_of(fdtv->device, struct unit_directory, device)->ne;
88}
89
Stefan Richter054286b2009-11-08 18:29:08 -030090static int node_lock(struct firedtv *fdtv, u64 addr, __be32 data[])
Stefan Richter15490792009-02-23 14:21:10 +010091{
Stefan Richter054286b2009-11-08 18:29:08 -030092 int ret;
93
94 ret = hpsb_node_lock(node_of(fdtv), addr, EXTCODE_COMPARE_SWAP,
95 (__force quadlet_t *)&data[1], (__force quadlet_t)data[0]);
96 data[0] = data[1];
97
98 return ret;
Stefan Richter15490792009-02-23 14:21:10 +010099}
100
101static int node_read(struct firedtv *fdtv, u64 addr, void *data, size_t len)
102{
103 return hpsb_node_read(node_of(fdtv), addr, data, len);
104}
105
106static int node_write(struct firedtv *fdtv, u64 addr, void *data, size_t len)
107{
108 return hpsb_node_write(node_of(fdtv), addr, data, len);
109}
110
111#define FDTV_ISO_BUFFER_PACKETS 256
112#define FDTV_ISO_BUFFER_SIZE (FDTV_ISO_BUFFER_PACKETS * 200)
113
114static int start_iso(struct firedtv *fdtv)
115{
116 struct hpsb_iso *iso_handle;
117 int ret;
118
119 iso_handle = hpsb_iso_recv_init(node_of(fdtv)->host,
120 FDTV_ISO_BUFFER_SIZE, FDTV_ISO_BUFFER_PACKETS,
121 fdtv->isochannel, HPSB_ISO_DMA_DEFAULT,
122 -1, /* stat.config.irq_interval */
123 rawiso_activity_cb);
124 if (iso_handle == NULL) {
125 dev_err(fdtv->device, "cannot initialize iso receive\n");
126 return -ENOMEM;
127 }
128 fdtv->backend_data = iso_handle;
129
130 ret = hpsb_iso_recv_start(iso_handle, -1, -1, 0);
131 if (ret != 0) {
132 dev_err(fdtv->device, "cannot start iso receive\n");
133 hpsb_iso_shutdown(iso_handle);
134 fdtv->backend_data = NULL;
135 }
136 return ret;
137}
138
139static void stop_iso(struct firedtv *fdtv)
140{
141 struct hpsb_iso *iso_handle = fdtv->backend_data;
142
143 if (iso_handle != NULL) {
144 hpsb_iso_stop(iso_handle);
145 hpsb_iso_shutdown(iso_handle);
146 }
147 fdtv->backend_data = NULL;
148}
149
150static const struct firedtv_backend fdtv_1394_backend = {
151 .lock = node_lock,
152 .read = node_read,
153 .write = node_write,
154 .start_iso = start_iso,
155 .stop_iso = stop_iso,
Rambaldia70f81c2009-01-17 14:47:34 +0100156};
157
Stefan Richter15490792009-02-23 14:21:10 +0100158static void fcp_request(struct hpsb_host *host, int nodeid, int direction,
159 int cts, u8 *data, size_t length)
160{
161 struct firedtv *f, *fdtv = NULL;
162 unsigned long flags;
163 int su;
164
165 if (length == 0 || (data[0] & 0xf0) != 0)
166 return;
167
168 su = data[1] & 0x7;
169
170 spin_lock_irqsave(&node_list_lock, flags);
171 list_for_each_entry(f, &node_list, list)
172 if (node_of(f)->host == host &&
173 node_of(f)->nodeid == nodeid &&
174 (f->subunit == su || (f->subunit == 0 && su == 0x7))) {
175 fdtv = f;
176 break;
177 }
178 spin_unlock_irqrestore(&node_list_lock, flags);
179
180 if (fdtv)
181 avc_recv(fdtv, data, length);
182}
183
184static int node_probe(struct device *dev)
Rambaldia70f81c2009-01-17 14:47:34 +0100185{
186 struct unit_directory *ud =
187 container_of(dev, struct unit_directory, device);
188 struct firedtv *fdtv;
Stefan Richter15490792009-02-23 14:21:10 +0100189 int kv_len, err;
Rambaldia70f81c2009-01-17 14:47:34 +0100190 void *kv_str;
Rambaldia70f81c2009-01-17 14:47:34 +0100191
Rambaldia70f81c2009-01-17 14:47:34 +0100192 kv_len = (ud->model_name_kv->value.leaf.len - 2) * sizeof(quadlet_t);
193 kv_str = CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(ud->model_name_kv);
Stefan Richter15490792009-02-23 14:21:10 +0100194
195 fdtv = fdtv_alloc(dev, &fdtv_1394_backend, kv_str, kv_len);
196 if (!fdtv)
197 return -ENOMEM;
Rambaldia70f81c2009-01-17 14:47:34 +0100198
199 /*
200 * Work around a bug in udev's path_id script: Use the fw-host's dev
201 * instead of the unit directory's dev as parent of the input device.
202 */
203 err = fdtv_register_rc(fdtv, dev->parent->parent);
204 if (err)
205 goto fail_free;
206
Stefan Richter15490792009-02-23 14:21:10 +0100207 spin_lock_irq(&node_list_lock);
208 list_add_tail(&fdtv->list, &node_list);
209 spin_unlock_irq(&node_list_lock);
Rambaldia70f81c2009-01-17 14:47:34 +0100210
211 err = avc_identify_subunit(fdtv);
212 if (err)
213 goto fail;
214
Stefan Richter15490792009-02-23 14:21:10 +0100215 err = fdtv_dvb_register(fdtv);
Rambaldia70f81c2009-01-17 14:47:34 +0100216 if (err)
217 goto fail;
218
219 avc_register_remote_control(fdtv);
Stefan Richter56411f42009-11-08 18:28:45 -0300220
Rambaldia70f81c2009-01-17 14:47:34 +0100221 return 0;
Rambaldia70f81c2009-01-17 14:47:34 +0100222fail:
Stefan Richter15490792009-02-23 14:21:10 +0100223 spin_lock_irq(&node_list_lock);
Rambaldia70f81c2009-01-17 14:47:34 +0100224 list_del(&fdtv->list);
Stefan Richter15490792009-02-23 14:21:10 +0100225 spin_unlock_irq(&node_list_lock);
Rambaldia70f81c2009-01-17 14:47:34 +0100226 fdtv_unregister_rc(fdtv);
227fail_free:
228 kfree(fdtv);
Stefan Richter56411f42009-11-08 18:28:45 -0300229
Rambaldia70f81c2009-01-17 14:47:34 +0100230 return err;
231}
232
Stefan Richter15490792009-02-23 14:21:10 +0100233static int node_remove(struct device *dev)
Rambaldia70f81c2009-01-17 14:47:34 +0100234{
Greg Kroah-Hartman79510cd2009-04-30 14:43:31 -0700235 struct firedtv *fdtv = dev_get_drvdata(dev);
Rambaldia70f81c2009-01-17 14:47:34 +0100236
Stefan Richter15490792009-02-23 14:21:10 +0100237 fdtv_dvb_unregister(fdtv);
Rambaldia70f81c2009-01-17 14:47:34 +0100238
Stefan Richter15490792009-02-23 14:21:10 +0100239 spin_lock_irq(&node_list_lock);
Rambaldia70f81c2009-01-17 14:47:34 +0100240 list_del(&fdtv->list);
Stefan Richter15490792009-02-23 14:21:10 +0100241 spin_unlock_irq(&node_list_lock);
Rambaldia70f81c2009-01-17 14:47:34 +0100242
Rambaldia70f81c2009-01-17 14:47:34 +0100243 fdtv_unregister_rc(fdtv);
Rambaldia70f81c2009-01-17 14:47:34 +0100244 kfree(fdtv);
Stefan Richter56411f42009-11-08 18:28:45 -0300245
Rambaldia70f81c2009-01-17 14:47:34 +0100246 return 0;
247}
248
Stefan Richter15490792009-02-23 14:21:10 +0100249static int node_update(struct unit_directory *ud)
Rambaldia70f81c2009-01-17 14:47:34 +0100250{
Greg Kroah-Hartman79510cd2009-04-30 14:43:31 -0700251 struct firedtv *fdtv = dev_get_drvdata(&ud->device);
Rambaldia70f81c2009-01-17 14:47:34 +0100252
253 if (fdtv->isochannel >= 0)
254 cmp_establish_pp_connection(fdtv, fdtv->subunit,
255 fdtv->isochannel);
256 return 0;
257}
258
259static struct hpsb_protocol_driver fdtv_driver = {
Rambaldia70f81c2009-01-17 14:47:34 +0100260 .name = "firedtv",
Stefan Richter15490792009-02-23 14:21:10 +0100261 .update = node_update,
Rambaldia70f81c2009-01-17 14:47:34 +0100262 .driver = {
Stefan Richter15490792009-02-23 14:21:10 +0100263 .probe = node_probe,
264 .remove = node_remove,
Rambaldia70f81c2009-01-17 14:47:34 +0100265 },
266};
267
268static struct hpsb_highlevel fdtv_highlevel = {
269 .name = "firedtv",
270 .fcp_request = fcp_request,
271};
272
Stefan Richter15490792009-02-23 14:21:10 +0100273int __init fdtv_1394_init(struct ieee1394_device_id id_table[])
Rambaldia70f81c2009-01-17 14:47:34 +0100274{
275 int ret;
276
277 hpsb_register_highlevel(&fdtv_highlevel);
Stefan Richter15490792009-02-23 14:21:10 +0100278 fdtv_driver.id_table = id_table;
Rambaldia70f81c2009-01-17 14:47:34 +0100279 ret = hpsb_register_protocol(&fdtv_driver);
280 if (ret) {
281 printk(KERN_ERR "firedtv: failed to register protocol\n");
282 hpsb_unregister_highlevel(&fdtv_highlevel);
283 }
284 return ret;
285}
286
Stefan Richter15490792009-02-23 14:21:10 +0100287void __exit fdtv_1394_exit(void)
Rambaldia70f81c2009-01-17 14:47:34 +0100288{
289 hpsb_unregister_protocol(&fdtv_driver);
290 hpsb_unregister_highlevel(&fdtv_highlevel);
291}