2 * FireDTV driver -- firewire I/O backend
5 #include <linux/device.h>
6 #include <linux/errno.h>
7 #include <linux/firewire.h>
8 #include <linux/firewire-constants.h>
9 #include <linux/kernel.h>
10 #include <linux/list.h>
12 #include <linux/slab.h>
13 #include <linux/spinlock.h>
14 #include <linux/types.h>
18 #include <dvb_demux.h>
22 static LIST_HEAD(node_list);
23 static DEFINE_SPINLOCK(node_list_lock);
25 static inline struct fw_device *device_of(struct firedtv *fdtv)
27 return fw_device(fdtv->device->parent);
30 static int node_req(struct firedtv *fdtv, u64 addr, void *data, size_t len,
33 struct fw_device *device = device_of(fdtv);
34 int rcode, generation = device->generation;
36 smp_rmb(); /* node_id vs. generation */
38 rcode = fw_run_transaction(device->card, tcode, device->node_id,
39 generation, device->max_speed, addr, data, len);
41 return rcode != RCODE_COMPLETE ? -EIO : 0;
44 static int node_lock(struct firedtv *fdtv, u64 addr, void *data)
46 return node_req(fdtv, addr, data, 8, TCODE_LOCK_COMPARE_SWAP);
49 static int node_read(struct firedtv *fdtv, u64 addr, void *data)
51 return node_req(fdtv, addr, data, 4, TCODE_READ_QUADLET_REQUEST);
54 static int node_write(struct firedtv *fdtv, u64 addr, void *data, size_t len)
56 return node_req(fdtv, addr, data, len, TCODE_WRITE_BLOCK_REQUEST);
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)
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
70 struct firedtv_receive_context {
71 struct fw_iso_context *context;
72 struct fw_iso_buffer buffer;
78 static int queue_iso(struct firedtv_receive_context *ctx, int index)
80 struct fw_iso_packet p;
82 p.payload_length = MAX_PACKET_SIZE;
83 p.interrupt = !(++ctx->interrupt_packet & (IRQ_INTERVAL - 1));
85 p.header_length = ISO_HEADER_SIZE;
87 return fw_iso_context_queue(ctx->context, &p, &ctx->buffer,
88 index * MAX_PACKET_SIZE);
91 static void handle_iso(struct fw_iso_context *context, u32 cycle,
92 size_t header_length, void *header, void *data)
94 struct firedtv *fdtv = data;
95 struct firedtv_receive_context *ctx = fdtv->backend_data;
97 int length, err, i = ctx->current_packet;
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;
107 p = ctx->pages[i / PACKETS_PER_PAGE]
108 + (i % PACKETS_PER_PAGE) * MAX_PACKET_SIZE;
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);
115 err = queue_iso(ctx, i);
117 dev_err(fdtv->device, "requeue failed\n");
119 i = (i + 1) & (N_PACKETS - 1);
121 ctx->current_packet = i;
124 static int start_iso(struct firedtv *fdtv)
126 struct firedtv_receive_context *ctx;
127 struct fw_device *device = device_of(fdtv);
130 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
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);
142 err = fw_iso_buffer_init(&ctx->buffer, device->card,
143 N_PAGES, DMA_FROM_DEVICE);
145 goto fail_context_destroy;
147 ctx->interrupt_packet = 0;
148 ctx->current_packet = 0;
150 for (i = 0; i < N_PAGES; i++)
151 ctx->pages[i] = page_address(ctx->buffer.pages[i]);
153 for (i = 0; i < N_PACKETS; i++) {
154 err = queue_iso(ctx, i);
159 err = fw_iso_context_start(ctx->context, -1, 0,
160 FW_ISO_CONTEXT_MATCH_ALL_TAGS);
164 fdtv->backend_data = ctx;
168 fw_iso_buffer_destroy(&ctx->buffer, device->card);
169 fail_context_destroy:
170 fw_iso_context_destroy(ctx->context);
177 static void stop_iso(struct firedtv *fdtv)
179 struct firedtv_receive_context *ctx = fdtv->backend_data;
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);
187 static const struct firedtv_backend backend = {
191 .start_iso = start_iso,
192 .stop_iso = stop_iso,
195 static void handle_fcp(struct fw_card *card, struct fw_request *request,
196 int tcode, int destination, int source, int generation,
197 int speed, unsigned long long offset,
198 void *payload, size_t length, void *callback_data)
200 struct firedtv *f, *fdtv = NULL;
201 struct fw_device *device;
205 if (length < 2 || (((u8 *)payload)[0] & 0xf0) != 0)
208 su = ((u8 *)payload)[1] & 0x7;
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)
216 smp_rmb(); /* node_id vs. generation */
218 if (device->card == card &&
219 device->node_id == source &&
220 (f->subunit == su || (f->subunit == 0 && su == 0x7))) {
225 spin_unlock_irqrestore(&node_list_lock, flags);
228 avc_recv(fdtv, payload, length);
231 static struct fw_address_handler fcp_handler = {
232 .length = CSR_FCP_END - CSR_FCP_RESPONSE,
233 .address_callback = handle_fcp,
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,
241 /* Adjust the template string if models with longer names appear. */
242 #define MAX_MODEL_NAME_LEN ((int)DIV_ROUND_UP(sizeof("FireDTV ????"), 4))
244 static size_t model_name(u32 *directory, __be32 *buffer)
246 struct fw_csr_iterator ci;
247 int i, length, key, value, last_key = 0;
250 fw_csr_iterator_init(&ci, directory);
251 while (fw_csr_iterator_next(&ci, &key, &value)) {
252 if (last_key == CSR_MODEL &&
253 key == (CSR_DESCRIPTOR | CSR_LEAF))
254 block = ci.p - 1 + value;
261 length = min((int)(block[0] >> 16) - 2, MAX_MODEL_NAME_LEN);
265 /* fast-forward to text string */
268 for (i = 0; i < length; i++)
269 buffer[i] = cpu_to_be32(block[i]);
274 static int node_probe(struct device *dev)
276 struct firedtv *fdtv;
277 __be32 name[MAX_MODEL_NAME_LEN];
280 name_len = model_name(fw_unit(dev)->directory, name);
282 fdtv = fdtv_alloc(dev, &backend, (char *)name, name_len);
286 err = fdtv_register_rc(fdtv, dev);
290 spin_lock_irq(&node_list_lock);
291 list_add_tail(&fdtv->list, &node_list);
292 spin_unlock_irq(&node_list_lock);
294 err = avc_identify_subunit(fdtv);
298 err = fdtv_dvb_register(fdtv);
302 avc_register_remote_control(fdtv);
306 spin_lock_irq(&node_list_lock);
307 list_del(&fdtv->list);
308 spin_unlock_irq(&node_list_lock);
309 fdtv_unregister_rc(fdtv);
316 static int node_remove(struct device *dev)
318 struct firedtv *fdtv = dev_get_drvdata(dev);
320 fdtv_dvb_unregister(fdtv);
322 spin_lock_irq(&node_list_lock);
323 list_del(&fdtv->list);
324 spin_unlock_irq(&node_list_lock);
326 fdtv_unregister_rc(fdtv);
332 static void node_update(struct fw_unit *unit)
334 struct firedtv *fdtv = dev_get_drvdata(&unit->device);
336 if (fdtv->isochannel >= 0)
337 cmp_establish_pp_connection(fdtv, fdtv->subunit,
341 static struct fw_driver fdtv_driver = {
343 .owner = THIS_MODULE,
347 .remove = node_remove,
349 .update = node_update,
350 .id_table = fdtv_id_table,
353 int __init fdtv_fw_init(void)
357 ret = fw_core_add_address_handler(&fcp_handler, &fcp_region);
361 return driver_register(&fdtv_driver.driver);
364 void fdtv_fw_exit(void)
366 driver_unregister(&fdtv_driver.driver);
367 fw_core_remove_address_handler(&fcp_handler);