]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/media/dvb/firewire/firedtv-1394.c
c3e0ec2dcfca56641b680162e5420ed976d19e4c
[net-next-2.6.git] / drivers / media / dvb / firewire / firedtv-1394.c
1 /*
2  * FireDTV driver -- ieee1394 I/O backend
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>
18 #include <linux/spinlock.h>
19 #include <linux/types.h>
20
21 #include <dma.h>
22 #include <csr1212.h>
23 #include <highlevel.h>
24 #include <hosts.h>
25 #include <ieee1394.h>
26 #include <iso.h>
27 #include <nodemgr.h>
28
29 #include <dvb_demux.h>
30
31 #include "firedtv.h"
32
33 static LIST_HEAD(node_list);
34 static DEFINE_SPINLOCK(node_list_lock);
35
36 #define CIP_HEADER_SIZE                 8
37 #define MPEG2_TS_HEADER_SIZE            4
38 #define MPEG2_TS_SOURCE_PACKET_SIZE     (4 + 188)
39
40 static void rawiso_activity_cb(struct hpsb_iso *iso)
41 {
42         struct firedtv *f, *fdtv = NULL;
43         unsigned int i, num, packet;
44         unsigned char *buf;
45         unsigned long flags;
46         int count;
47
48         spin_lock_irqsave(&node_list_lock, flags);
49         list_for_each_entry(f, &node_list, list)
50                 if (f->backend_data == iso) {
51                         fdtv = f;
52                         break;
53                 }
54         spin_unlock_irqrestore(&node_list_lock, flags);
55
56         packet = iso->first_packet;
57         num = hpsb_iso_n_ready(iso);
58
59         if (!fdtv) {
60                 dev_err(fdtv->device, "received at unknown iso channel\n");
61                 goto out;
62         }
63
64         for (i = 0; i < num; i++, packet = (packet + 1) % iso->buf_packets) {
65                 buf = dma_region_i(&iso->data_buf, unsigned char,
66                         iso->infos[packet].offset + CIP_HEADER_SIZE);
67                 count = (iso->infos[packet].len - CIP_HEADER_SIZE) /
68                         MPEG2_TS_SOURCE_PACKET_SIZE;
69
70                 /* ignore empty packet */
71                 if (iso->infos[packet].len <= CIP_HEADER_SIZE)
72                         continue;
73
74                 while (count--) {
75                         if (buf[MPEG2_TS_HEADER_SIZE] == 0x47)
76                                 dvb_dmx_swfilter_packets(&fdtv->demux,
77                                                 &buf[MPEG2_TS_HEADER_SIZE], 1);
78                         else
79                                 dev_err(fdtv->device,
80                                         "skipping invalid packet\n");
81                         buf += MPEG2_TS_SOURCE_PACKET_SIZE;
82                 }
83         }
84 out:
85         hpsb_iso_recv_release_packets(iso, num);
86 }
87
88 static inline struct node_entry *node_of(struct firedtv *fdtv)
89 {
90         return container_of(fdtv->device, struct unit_directory, device)->ne;
91 }
92
93 static int node_lock(struct firedtv *fdtv, u64 addr, void *data)
94 {
95         quadlet_t *d = data;
96         int ret;
97
98         ret = hpsb_node_lock(node_of(fdtv), addr,
99                              EXTCODE_COMPARE_SWAP, &d[1], d[0]);
100         d[0] = d[1];
101
102         return ret;
103 }
104
105 static int node_read(struct firedtv *fdtv, u64 addr, void *data)
106 {
107         return hpsb_node_read(node_of(fdtv), addr, data, 4);
108 }
109
110 static int node_write(struct firedtv *fdtv, u64 addr, void *data, size_t len)
111 {
112         return hpsb_node_write(node_of(fdtv), addr, data, len);
113 }
114
115 #define FDTV_ISO_BUFFER_PACKETS 256
116 #define FDTV_ISO_BUFFER_SIZE (FDTV_ISO_BUFFER_PACKETS * 200)
117
118 static int start_iso(struct firedtv *fdtv)
119 {
120         struct hpsb_iso *iso_handle;
121         int ret;
122
123         iso_handle = hpsb_iso_recv_init(node_of(fdtv)->host,
124                                 FDTV_ISO_BUFFER_SIZE, FDTV_ISO_BUFFER_PACKETS,
125                                 fdtv->isochannel, HPSB_ISO_DMA_DEFAULT,
126                                 -1, /* stat.config.irq_interval */
127                                 rawiso_activity_cb);
128         if (iso_handle == NULL) {
129                 dev_err(fdtv->device, "cannot initialize iso receive\n");
130                 return -ENOMEM;
131         }
132         fdtv->backend_data = iso_handle;
133
134         ret = hpsb_iso_recv_start(iso_handle, -1, -1, 0);
135         if (ret != 0) {
136                 dev_err(fdtv->device, "cannot start iso receive\n");
137                 hpsb_iso_shutdown(iso_handle);
138                 fdtv->backend_data = NULL;
139         }
140         return ret;
141 }
142
143 static void stop_iso(struct firedtv *fdtv)
144 {
145         struct hpsb_iso *iso_handle = fdtv->backend_data;
146
147         if (iso_handle != NULL) {
148                 hpsb_iso_stop(iso_handle);
149                 hpsb_iso_shutdown(iso_handle);
150         }
151         fdtv->backend_data = NULL;
152 }
153
154 static const struct firedtv_backend fdtv_1394_backend = {
155         .lock           = node_lock,
156         .read           = node_read,
157         .write          = node_write,
158         .start_iso      = start_iso,
159         .stop_iso       = stop_iso,
160 };
161
162 static void fcp_request(struct hpsb_host *host, int nodeid, int direction,
163                         int cts, u8 *data, size_t length)
164 {
165         struct firedtv *f, *fdtv = NULL;
166         unsigned long flags;
167         int su;
168
169         if (length == 0 || (data[0] & 0xf0) != 0)
170                 return;
171
172         su = data[1] & 0x7;
173
174         spin_lock_irqsave(&node_list_lock, flags);
175         list_for_each_entry(f, &node_list, list)
176                 if (node_of(f)->host == host &&
177                     node_of(f)->nodeid == nodeid &&
178                     (f->subunit == su || (f->subunit == 0 && su == 0x7))) {
179                         fdtv = f;
180                         break;
181                 }
182         spin_unlock_irqrestore(&node_list_lock, flags);
183
184         if (fdtv)
185                 avc_recv(fdtv, data, length);
186 }
187
188 static int node_probe(struct device *dev)
189 {
190         struct unit_directory *ud =
191                         container_of(dev, struct unit_directory, device);
192         struct firedtv *fdtv;
193         int kv_len, err;
194         void *kv_str;
195
196         if (ud->model_name_kv) {
197                 kv_len = (ud->model_name_kv->value.leaf.len - 2) * 4;
198                 kv_str = CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(ud->model_name_kv);
199         } else {
200                 kv_len = 0;
201                 kv_str = NULL;
202         }
203         fdtv = fdtv_alloc(dev, &fdtv_1394_backend, kv_str, kv_len);
204         if (!fdtv)
205                 return -ENOMEM;
206
207         /*
208          * Work around a bug in udev's path_id script:  Use the fw-host's dev
209          * instead of the unit directory's dev as parent of the input device.
210          */
211         err = fdtv_register_rc(fdtv, dev->parent->parent);
212         if (err)
213                 goto fail_free;
214
215         spin_lock_irq(&node_list_lock);
216         list_add_tail(&fdtv->list, &node_list);
217         spin_unlock_irq(&node_list_lock);
218
219         err = avc_identify_subunit(fdtv);
220         if (err)
221                 goto fail;
222
223         err = fdtv_dvb_register(fdtv);
224         if (err)
225                 goto fail;
226
227         avc_register_remote_control(fdtv);
228
229         return 0;
230 fail:
231         spin_lock_irq(&node_list_lock);
232         list_del(&fdtv->list);
233         spin_unlock_irq(&node_list_lock);
234         fdtv_unregister_rc(fdtv);
235 fail_free:
236         kfree(fdtv);
237
238         return err;
239 }
240
241 static int node_remove(struct device *dev)
242 {
243         struct firedtv *fdtv = dev_get_drvdata(dev);
244
245         fdtv_dvb_unregister(fdtv);
246
247         spin_lock_irq(&node_list_lock);
248         list_del(&fdtv->list);
249         spin_unlock_irq(&node_list_lock);
250
251         fdtv_unregister_rc(fdtv);
252         kfree(fdtv);
253
254         return 0;
255 }
256
257 static int node_update(struct unit_directory *ud)
258 {
259         struct firedtv *fdtv = dev_get_drvdata(&ud->device);
260
261         if (fdtv->isochannel >= 0)
262                 cmp_establish_pp_connection(fdtv, fdtv->subunit,
263                                             fdtv->isochannel);
264         return 0;
265 }
266
267 static struct hpsb_protocol_driver fdtv_driver = {
268         .name           = "firedtv",
269         .id_table       = fdtv_id_table,
270         .update         = node_update,
271         .driver         = {
272                 .probe  = node_probe,
273                 .remove = node_remove,
274         },
275 };
276
277 static struct hpsb_highlevel fdtv_highlevel = {
278         .name           = "firedtv",
279         .fcp_request    = fcp_request,
280 };
281
282 int __init fdtv_1394_init(void)
283 {
284         int ret;
285
286         hpsb_register_highlevel(&fdtv_highlevel);
287         ret = hpsb_register_protocol(&fdtv_driver);
288         if (ret) {
289                 printk(KERN_ERR "firedtv: failed to register protocol\n");
290                 hpsb_unregister_highlevel(&fdtv_highlevel);
291         }
292         return ret;
293 }
294
295 void __exit fdtv_1394_exit(void)
296 {
297         hpsb_unregister_protocol(&fdtv_driver);
298         hpsb_unregister_highlevel(&fdtv_highlevel);
299 }