blob: 6954848b58034b27649257597a898dee4643e023 [file] [log] [blame]
Stefan Richter87918332009-11-08 18:30:54 -03001/*
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 Richter87918332009-11-08 18:30:54 -03009#include <linux/kernel.h>
10#include <linux/list.h>
Stefan Richtera8aeb782009-11-18 16:00:55 -030011#include <linux/mm.h>
Stefan Richter87918332009-11-08 18:30:54 -030012#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
22static LIST_HEAD(node_list);
23static DEFINE_SPINLOCK(node_list_lock);
24
25static inline struct fw_device *device_of(struct firedtv *fdtv)
26{
27 return fw_device(fdtv->device->parent);
28}
29
30static 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
44static int node_lock(struct firedtv *fdtv, u64 addr, __be32 data[])
45{
46 return node_req(fdtv, addr, data, 8, TCODE_LOCK_COMPARE_SWAP);
47}
48
49static int node_read(struct firedtv *fdtv, u64 addr, void *data, size_t len)
50{
51 return node_req(fdtv, addr, data, len, len == 4 ?
52 TCODE_READ_QUADLET_REQUEST : TCODE_READ_BLOCK_REQUEST);
53}
54
55static int node_write(struct firedtv *fdtv, u64 addr, void *data, size_t len)
56{
57 return node_req(fdtv, addr, data, len, TCODE_WRITE_BLOCK_REQUEST);
58}
59
60#define ISO_HEADER_SIZE 4
61#define CIP_HEADER_SIZE 8
62#define MPEG2_TS_HEADER_SIZE 4
63#define MPEG2_TS_SOURCE_PACKET_SIZE (4 + 188)
64
65#define MAX_PACKET_SIZE 1024 /* 776, rounded up to 2^n */
66#define PACKETS_PER_PAGE (PAGE_SIZE / MAX_PACKET_SIZE)
67#define N_PACKETS 64 /* buffer size */
68#define N_PAGES DIV_ROUND_UP(N_PACKETS, PACKETS_PER_PAGE)
69#define IRQ_INTERVAL 16
70
71struct firedtv_receive_context {
72 struct fw_iso_context *context;
73 struct fw_iso_buffer buffer;
74 int interrupt_packet;
75 int current_packet;
Stefan Richtera8aeb782009-11-18 16:00:55 -030076 char *pages[N_PAGES];
Stefan Richter87918332009-11-08 18:30:54 -030077};
78
79static int queue_iso(struct firedtv_receive_context *ctx, int index)
80{
81 struct fw_iso_packet p;
82 int err;
83
84 p.payload_length = MAX_PACKET_SIZE;
85 p.interrupt = !(ctx->interrupt_packet & (IRQ_INTERVAL - 1));
86 p.skip = 0;
87 p.header_length = ISO_HEADER_SIZE;
88
89 err = fw_iso_context_queue(ctx->context, &p, &ctx->buffer,
90 index * MAX_PACKET_SIZE);
91 if (!err)
92 ctx->interrupt_packet++;
93
94 return err;
95}
96
97static void handle_iso(struct fw_iso_context *context, u32 cycle,
98 size_t header_length, void *header, void *data)
99{
100 struct firedtv *fdtv = data;
101 struct firedtv_receive_context *ctx = fdtv->backend_data;
102 __be32 *h, *h_end;
Stefan Richtera8aeb782009-11-18 16:00:55 -0300103 int length, err, i = ctx->current_packet;
Stefan Richter87918332009-11-08 18:30:54 -0300104 char *p, *p_end;
105
106 for (h = header, h_end = h + header_length / 4; h < h_end; h++) {
107 length = be32_to_cpup(h) >> 16;
108 if (unlikely(length > MAX_PACKET_SIZE)) {
109 dev_err(fdtv->device, "length = %d\n", length);
110 length = MAX_PACKET_SIZE;
111 }
112
Stefan Richtera8aeb782009-11-18 16:00:55 -0300113 p = ctx->pages[i / PACKETS_PER_PAGE]
114 + (i % PACKETS_PER_PAGE) * MAX_PACKET_SIZE;
Stefan Richter87918332009-11-08 18:30:54 -0300115 p_end = p + length;
116
117 for (p += CIP_HEADER_SIZE + MPEG2_TS_HEADER_SIZE; p < p_end;
118 p += MPEG2_TS_SOURCE_PACKET_SIZE)
119 dvb_dmx_swfilter_packets(&fdtv->demux, p, 1);
120
121 err = queue_iso(ctx, i);
122 if (unlikely(err))
123 dev_err(fdtv->device, "requeue failed\n");
124
125 i = (i + 1) & (N_PACKETS - 1);
126 }
127 ctx->current_packet = i;
128}
129
130static int start_iso(struct firedtv *fdtv)
131{
132 struct firedtv_receive_context *ctx;
133 struct fw_device *device = device_of(fdtv);
Stefan Richtera8aeb782009-11-18 16:00:55 -0300134 int i, err;
Stefan Richter87918332009-11-08 18:30:54 -0300135
136 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
137 if (!ctx)
138 return -ENOMEM;
139
140 ctx->context = fw_iso_context_create(device->card,
141 FW_ISO_CONTEXT_RECEIVE, fdtv->isochannel,
142 device->max_speed, ISO_HEADER_SIZE, handle_iso, fdtv);
143 if (IS_ERR(ctx->context)) {
144 err = PTR_ERR(ctx->context);
145 goto fail_free;
146 }
147
148 err = fw_iso_buffer_init(&ctx->buffer, device->card,
149 N_PAGES, DMA_FROM_DEVICE);
150 if (err)
151 goto fail_context_destroy;
152
153 ctx->interrupt_packet = 1;
154 ctx->current_packet = 0;
155
Stefan Richtera8aeb782009-11-18 16:00:55 -0300156 for (i = 0; i < N_PAGES; i++)
157 ctx->pages[i] = page_address(ctx->buffer.pages[i]);
Stefan Richter87918332009-11-08 18:30:54 -0300158
159 for (i = 0; i < N_PACKETS; i++) {
160 err = queue_iso(ctx, i);
161 if (err)
162 goto fail;
163 }
164
165 err = fw_iso_context_start(ctx->context, -1, 0,
166 FW_ISO_CONTEXT_MATCH_ALL_TAGS);
167 if (err)
168 goto fail;
169
170 fdtv->backend_data = ctx;
171
172 return 0;
173fail:
174 fw_iso_buffer_destroy(&ctx->buffer, device->card);
175fail_context_destroy:
176 fw_iso_context_destroy(ctx->context);
177fail_free:
178 kfree(ctx);
179
180 return err;
181}
182
183static void stop_iso(struct firedtv *fdtv)
184{
185 struct firedtv_receive_context *ctx = fdtv->backend_data;
186
187 fw_iso_context_stop(ctx->context);
188 fw_iso_buffer_destroy(&ctx->buffer, device_of(fdtv)->card);
189 fw_iso_context_destroy(ctx->context);
190 kfree(ctx);
191}
192
193static const struct firedtv_backend backend = {
194 .lock = node_lock,
195 .read = node_read,
196 .write = node_write,
197 .start_iso = start_iso,
198 .stop_iso = stop_iso,
199};
200
201static void handle_fcp(struct fw_card *card, struct fw_request *request,
202 int tcode, int destination, int source, int generation,
203 int speed, unsigned long long offset,
204 void *payload, size_t length, void *callback_data)
205{
206 struct firedtv *f, *fdtv = NULL;
207 struct fw_device *device;
208 unsigned long flags;
209 int su;
210
211 if ((tcode != TCODE_WRITE_QUADLET_REQUEST &&
212 tcode != TCODE_WRITE_BLOCK_REQUEST) ||
213 offset != CSR_REGISTER_BASE + CSR_FCP_RESPONSE ||
214 length == 0 ||
215 (((u8 *)payload)[0] & 0xf0) != 0) {
216 fw_send_response(card, request, RCODE_TYPE_ERROR);
217 return;
218 }
219
220 su = ((u8 *)payload)[1] & 0x7;
221
222 spin_lock_irqsave(&node_list_lock, flags);
223 list_for_each_entry(f, &node_list, list) {
224 device = device_of(f);
225 if (device->generation != generation)
226 continue;
227
228 smp_rmb(); /* node_id vs. generation */
229
230 if (device->card == card &&
231 device->node_id == source &&
232 (f->subunit == su || (f->subunit == 0 && su == 0x7))) {
233 fdtv = f;
234 break;
235 }
236 }
237 spin_unlock_irqrestore(&node_list_lock, flags);
238
239 if (fdtv) {
240 avc_recv(fdtv, payload, length);
241 fw_send_response(card, request, RCODE_COMPLETE);
242 }
243}
244
245static struct fw_address_handler fcp_handler = {
246 .length = CSR_FCP_END - CSR_FCP_RESPONSE,
247 .address_callback = handle_fcp,
248};
249
250static const struct fw_address_region fcp_region = {
251 .start = CSR_REGISTER_BASE + CSR_FCP_RESPONSE,
252 .end = CSR_REGISTER_BASE + CSR_FCP_END,
253};
254
255/* Adjust the template string if models with longer names appear. */
256#define MAX_MODEL_NAME_LEN ((int)DIV_ROUND_UP(sizeof("FireDTV ????"), 4))
257
258static size_t model_name(u32 *directory, __be32 *buffer)
259{
260 struct fw_csr_iterator ci;
261 int i, length, key, value, last_key = 0;
262 u32 *block = NULL;
263
264 fw_csr_iterator_init(&ci, directory);
265 while (fw_csr_iterator_next(&ci, &key, &value)) {
266 if (last_key == CSR_MODEL &&
267 key == (CSR_DESCRIPTOR | CSR_LEAF))
268 block = ci.p - 1 + value;
269 last_key = key;
270 }
271
272 if (block == NULL)
273 return 0;
274
275 length = min((int)(block[0] >> 16) - 2, MAX_MODEL_NAME_LEN);
276 if (length <= 0)
277 return 0;
278
279 /* fast-forward to text string */
280 block += 3;
281
282 for (i = 0; i < length; i++)
283 buffer[i] = cpu_to_be32(block[i]);
284
285 return length * 4;
286}
287
288static int node_probe(struct device *dev)
289{
290 struct firedtv *fdtv;
291 __be32 name[MAX_MODEL_NAME_LEN];
292 int name_len, err;
293
294 name_len = model_name(fw_unit(dev)->directory, name);
295
296 fdtv = fdtv_alloc(dev, &backend, (char *)name, name_len);
297 if (!fdtv)
298 return -ENOMEM;
299
300 err = fdtv_register_rc(fdtv, dev);
301 if (err)
302 goto fail_free;
303
304 spin_lock_irq(&node_list_lock);
305 list_add_tail(&fdtv->list, &node_list);
306 spin_unlock_irq(&node_list_lock);
307
308 err = avc_identify_subunit(fdtv);
309 if (err)
310 goto fail;
311
312 err = fdtv_dvb_register(fdtv);
313 if (err)
314 goto fail;
315
316 avc_register_remote_control(fdtv);
317
318 return 0;
319fail:
320 spin_lock_irq(&node_list_lock);
321 list_del(&fdtv->list);
322 spin_unlock_irq(&node_list_lock);
323 fdtv_unregister_rc(fdtv);
324fail_free:
325 kfree(fdtv);
326
327 return err;
328}
329
330static int node_remove(struct device *dev)
331{
332 struct firedtv *fdtv = dev_get_drvdata(dev);
333
334 fdtv_dvb_unregister(fdtv);
335
336 spin_lock_irq(&node_list_lock);
337 list_del(&fdtv->list);
338 spin_unlock_irq(&node_list_lock);
339
340 fdtv_unregister_rc(fdtv);
341
342 kfree(fdtv);
343 return 0;
344}
345
346static void node_update(struct fw_unit *unit)
347{
348 struct firedtv *fdtv = dev_get_drvdata(&unit->device);
349
350 if (fdtv->isochannel >= 0)
351 cmp_establish_pp_connection(fdtv, fdtv->subunit,
352 fdtv->isochannel);
353}
354
355static struct fw_driver fdtv_driver = {
356 .driver = {
357 .owner = THIS_MODULE,
358 .name = "firedtv",
359 .bus = &fw_bus_type,
360 .probe = node_probe,
361 .remove = node_remove,
362 },
363 .update = node_update,
364 .id_table = fdtv_id_table,
365};
366
367int __init fdtv_fw_init(void)
368{
369 int ret;
370
371 ret = fw_core_add_address_handler(&fcp_handler, &fcp_region);
372 if (ret < 0)
373 return ret;
374
375 return driver_register(&fdtv_driver.driver);
376}
377
378void fdtv_fw_exit(void)
379{
380 driver_unregister(&fdtv_driver.driver);
381 fw_core_remove_address_handler(&fcp_handler);
382}