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