Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * IEEE 1394 for Linux |
| 3 | * |
| 4 | * kernel ISO transmission/reception |
| 5 | * |
| 6 | * Copyright (C) 2002 Maas Digital LLC |
| 7 | * |
| 8 | * This code is licensed under the GPL. See the file COPYING in the root |
| 9 | * directory of the kernel sources for details. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/sched.h> |
| 14 | #include "iso.h" |
| 15 | |
| 16 | void hpsb_iso_stop(struct hpsb_iso *iso) |
| 17 | { |
| 18 | if (!(iso->flags & HPSB_ISO_DRIVER_STARTED)) |
| 19 | return; |
| 20 | |
| 21 | iso->host->driver->isoctl(iso, iso->type == HPSB_ISO_XMIT ? |
| 22 | XMIT_STOP : RECV_STOP, 0); |
| 23 | iso->flags &= ~HPSB_ISO_DRIVER_STARTED; |
| 24 | } |
| 25 | |
| 26 | void hpsb_iso_shutdown(struct hpsb_iso *iso) |
| 27 | { |
| 28 | if (iso->flags & HPSB_ISO_DRIVER_INIT) { |
| 29 | hpsb_iso_stop(iso); |
| 30 | iso->host->driver->isoctl(iso, iso->type == HPSB_ISO_XMIT ? |
| 31 | XMIT_SHUTDOWN : RECV_SHUTDOWN, 0); |
| 32 | iso->flags &= ~HPSB_ISO_DRIVER_INIT; |
| 33 | } |
| 34 | |
| 35 | dma_region_free(&iso->data_buf); |
| 36 | kfree(iso); |
| 37 | } |
| 38 | |
| 39 | static struct hpsb_iso* hpsb_iso_common_init(struct hpsb_host *host, enum hpsb_iso_type type, |
| 40 | unsigned int data_buf_size, |
| 41 | unsigned int buf_packets, |
| 42 | int channel, |
| 43 | int dma_mode, |
| 44 | int irq_interval, |
| 45 | void (*callback)(struct hpsb_iso*)) |
| 46 | { |
| 47 | struct hpsb_iso *iso; |
| 48 | int dma_direction; |
| 49 | |
| 50 | /* make sure driver supports the ISO API */ |
| 51 | if (!host->driver->isoctl) { |
| 52 | printk(KERN_INFO "ieee1394: host driver '%s' does not support the rawiso API\n", |
| 53 | host->driver->name); |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | /* sanitize parameters */ |
| 58 | |
| 59 | if (buf_packets < 2) |
| 60 | buf_packets = 2; |
| 61 | |
| 62 | if ((dma_mode < HPSB_ISO_DMA_DEFAULT) || (dma_mode > HPSB_ISO_DMA_PACKET_PER_BUFFER)) |
| 63 | dma_mode=HPSB_ISO_DMA_DEFAULT; |
| 64 | |
| 65 | if (irq_interval == 0) /* really interrupt for each packet*/ |
| 66 | irq_interval = 1; |
| 67 | else if ((irq_interval < 0) || (irq_interval > buf_packets / 4)) |
| 68 | irq_interval = buf_packets / 4; |
| 69 | |
| 70 | if (channel < -1 || channel >= 64) |
| 71 | return NULL; |
| 72 | |
| 73 | /* channel = -1 is OK for multi-channel recv but not for xmit */ |
| 74 | if (type == HPSB_ISO_XMIT && channel < 0) |
| 75 | return NULL; |
| 76 | |
| 77 | /* allocate and write the struct hpsb_iso */ |
| 78 | |
| 79 | iso = kmalloc(sizeof(*iso) + buf_packets * sizeof(struct hpsb_iso_packet_info), GFP_KERNEL); |
| 80 | if (!iso) |
| 81 | return NULL; |
| 82 | |
| 83 | iso->infos = (struct hpsb_iso_packet_info *)(iso + 1); |
| 84 | |
| 85 | iso->type = type; |
| 86 | iso->host = host; |
| 87 | iso->hostdata = NULL; |
| 88 | iso->callback = callback; |
| 89 | init_waitqueue_head(&iso->waitq); |
| 90 | iso->channel = channel; |
| 91 | iso->irq_interval = irq_interval; |
| 92 | iso->dma_mode = dma_mode; |
| 93 | dma_region_init(&iso->data_buf); |
| 94 | iso->buf_size = PAGE_ALIGN(data_buf_size); |
| 95 | iso->buf_packets = buf_packets; |
| 96 | iso->pkt_dma = 0; |
| 97 | iso->first_packet = 0; |
| 98 | spin_lock_init(&iso->lock); |
| 99 | |
| 100 | if (iso->type == HPSB_ISO_XMIT) { |
| 101 | iso->n_ready_packets = iso->buf_packets; |
| 102 | dma_direction = PCI_DMA_TODEVICE; |
| 103 | } else { |
| 104 | iso->n_ready_packets = 0; |
| 105 | dma_direction = PCI_DMA_FROMDEVICE; |
| 106 | } |
| 107 | |
| 108 | atomic_set(&iso->overflows, 0); |
| 109 | iso->flags = 0; |
| 110 | iso->prebuffer = 0; |
| 111 | |
| 112 | /* allocate the packet buffer */ |
| 113 | if (dma_region_alloc(&iso->data_buf, iso->buf_size, host->pdev, dma_direction)) |
| 114 | goto err; |
| 115 | |
| 116 | return iso; |
| 117 | |
| 118 | err: |
| 119 | hpsb_iso_shutdown(iso); |
| 120 | return NULL; |
| 121 | } |
| 122 | |
| 123 | int hpsb_iso_n_ready(struct hpsb_iso* iso) |
| 124 | { |
| 125 | unsigned long flags; |
| 126 | int val; |
| 127 | |
| 128 | spin_lock_irqsave(&iso->lock, flags); |
| 129 | val = iso->n_ready_packets; |
| 130 | spin_unlock_irqrestore(&iso->lock, flags); |
| 131 | |
| 132 | return val; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | struct hpsb_iso* hpsb_iso_xmit_init(struct hpsb_host *host, |
| 137 | unsigned int data_buf_size, |
| 138 | unsigned int buf_packets, |
| 139 | int channel, |
| 140 | int speed, |
| 141 | int irq_interval, |
| 142 | void (*callback)(struct hpsb_iso*)) |
| 143 | { |
| 144 | struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_XMIT, |
| 145 | data_buf_size, buf_packets, |
| 146 | channel, HPSB_ISO_DMA_DEFAULT, irq_interval, callback); |
| 147 | if (!iso) |
| 148 | return NULL; |
| 149 | |
| 150 | iso->speed = speed; |
| 151 | |
| 152 | /* tell the driver to start working */ |
| 153 | if (host->driver->isoctl(iso, XMIT_INIT, 0)) |
| 154 | goto err; |
| 155 | |
| 156 | iso->flags |= HPSB_ISO_DRIVER_INIT; |
| 157 | return iso; |
| 158 | |
| 159 | err: |
| 160 | hpsb_iso_shutdown(iso); |
| 161 | return NULL; |
| 162 | } |
| 163 | |
| 164 | struct hpsb_iso* hpsb_iso_recv_init(struct hpsb_host *host, |
| 165 | unsigned int data_buf_size, |
| 166 | unsigned int buf_packets, |
| 167 | int channel, |
| 168 | int dma_mode, |
| 169 | int irq_interval, |
| 170 | void (*callback)(struct hpsb_iso*)) |
| 171 | { |
| 172 | struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_RECV, |
| 173 | data_buf_size, buf_packets, |
| 174 | channel, dma_mode, irq_interval, callback); |
| 175 | if (!iso) |
| 176 | return NULL; |
| 177 | |
| 178 | /* tell the driver to start working */ |
| 179 | if (host->driver->isoctl(iso, RECV_INIT, 0)) |
| 180 | goto err; |
| 181 | |
| 182 | iso->flags |= HPSB_ISO_DRIVER_INIT; |
| 183 | return iso; |
| 184 | |
| 185 | err: |
| 186 | hpsb_iso_shutdown(iso); |
| 187 | return NULL; |
| 188 | } |
| 189 | |
| 190 | int hpsb_iso_recv_listen_channel(struct hpsb_iso *iso, unsigned char channel) |
| 191 | { |
| 192 | if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64) |
| 193 | return -EINVAL; |
| 194 | return iso->host->driver->isoctl(iso, RECV_LISTEN_CHANNEL, channel); |
| 195 | } |
| 196 | |
| 197 | int hpsb_iso_recv_unlisten_channel(struct hpsb_iso *iso, unsigned char channel) |
| 198 | { |
| 199 | if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64) |
| 200 | return -EINVAL; |
| 201 | return iso->host->driver->isoctl(iso, RECV_UNLISTEN_CHANNEL, channel); |
| 202 | } |
| 203 | |
| 204 | int hpsb_iso_recv_set_channel_mask(struct hpsb_iso *iso, u64 mask) |
| 205 | { |
| 206 | if (iso->type != HPSB_ISO_RECV || iso->channel != -1) |
| 207 | return -EINVAL; |
| 208 | return iso->host->driver->isoctl(iso, RECV_SET_CHANNEL_MASK, (unsigned long) &mask); |
| 209 | } |
| 210 | |
| 211 | int hpsb_iso_recv_flush(struct hpsb_iso *iso) |
| 212 | { |
| 213 | if (iso->type != HPSB_ISO_RECV) |
| 214 | return -EINVAL; |
| 215 | return iso->host->driver->isoctl(iso, RECV_FLUSH, 0); |
| 216 | } |
| 217 | |
| 218 | static int do_iso_xmit_start(struct hpsb_iso *iso, int cycle) |
| 219 | { |
| 220 | int retval = iso->host->driver->isoctl(iso, XMIT_START, cycle); |
| 221 | if (retval) |
| 222 | return retval; |
| 223 | |
| 224 | iso->flags |= HPSB_ISO_DRIVER_STARTED; |
| 225 | return retval; |
| 226 | } |
| 227 | |
| 228 | int hpsb_iso_xmit_start(struct hpsb_iso *iso, int cycle, int prebuffer) |
| 229 | { |
| 230 | if (iso->type != HPSB_ISO_XMIT) |
| 231 | return -1; |
| 232 | |
| 233 | if (iso->flags & HPSB_ISO_DRIVER_STARTED) |
| 234 | return 0; |
| 235 | |
| 236 | if (cycle < -1) |
| 237 | cycle = -1; |
| 238 | else if (cycle >= 8000) |
| 239 | cycle %= 8000; |
| 240 | |
| 241 | iso->xmit_cycle = cycle; |
| 242 | |
| 243 | if (prebuffer < 0) |
| 244 | prebuffer = iso->buf_packets; |
| 245 | else if (prebuffer == 0) |
| 246 | prebuffer = 1; |
| 247 | |
| 248 | if (prebuffer > iso->buf_packets) |
| 249 | prebuffer = iso->buf_packets; |
| 250 | |
| 251 | iso->prebuffer = prebuffer; |
| 252 | |
| 253 | /* remember the starting cycle; DMA will commence from xmit_queue_packets() |
| 254 | once enough packets have been buffered */ |
| 255 | iso->start_cycle = cycle; |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | int hpsb_iso_recv_start(struct hpsb_iso *iso, int cycle, int tag_mask, int sync) |
| 261 | { |
| 262 | int retval = 0; |
| 263 | int isoctl_args[3]; |
| 264 | |
| 265 | if (iso->type != HPSB_ISO_RECV) |
| 266 | return -1; |
| 267 | |
| 268 | if (iso->flags & HPSB_ISO_DRIVER_STARTED) |
| 269 | return 0; |
| 270 | |
| 271 | if (cycle < -1) |
| 272 | cycle = -1; |
| 273 | else if (cycle >= 8000) |
| 274 | cycle %= 8000; |
| 275 | |
| 276 | isoctl_args[0] = cycle; |
| 277 | |
| 278 | if (tag_mask < 0) |
| 279 | /* match all tags */ |
| 280 | tag_mask = 0xF; |
| 281 | isoctl_args[1] = tag_mask; |
| 282 | |
| 283 | isoctl_args[2] = sync; |
| 284 | |
| 285 | retval = iso->host->driver->isoctl(iso, RECV_START, (unsigned long) &isoctl_args[0]); |
| 286 | if (retval) |
| 287 | return retval; |
| 288 | |
| 289 | iso->flags |= HPSB_ISO_DRIVER_STARTED; |
| 290 | return retval; |
| 291 | } |
| 292 | |
| 293 | /* check to make sure the user has not supplied bogus values of offset/len |
| 294 | that would cause the kernel to access memory outside the buffer */ |
| 295 | |
| 296 | static int hpsb_iso_check_offset_len(struct hpsb_iso *iso, |
| 297 | unsigned int offset, unsigned short len, |
| 298 | unsigned int *out_offset, unsigned short *out_len) |
| 299 | { |
| 300 | if (offset >= iso->buf_size) |
| 301 | return -EFAULT; |
| 302 | |
| 303 | /* make sure the packet does not go beyond the end of the buffer */ |
| 304 | if (offset + len > iso->buf_size) |
| 305 | return -EFAULT; |
| 306 | |
| 307 | /* check for wrap-around */ |
| 308 | if (offset + len < offset) |
| 309 | return -EFAULT; |
| 310 | |
| 311 | /* now we can trust 'offset' and 'length' */ |
| 312 | *out_offset = offset; |
| 313 | *out_len = len; |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | |
| 319 | int hpsb_iso_xmit_queue_packet(struct hpsb_iso *iso, u32 offset, u16 len, u8 tag, u8 sy) |
| 320 | { |
| 321 | struct hpsb_iso_packet_info *info; |
| 322 | unsigned long flags; |
| 323 | int rv; |
| 324 | |
| 325 | if (iso->type != HPSB_ISO_XMIT) |
| 326 | return -EINVAL; |
| 327 | |
| 328 | /* is there space in the buffer? */ |
| 329 | if (iso->n_ready_packets <= 0) { |
| 330 | return -EBUSY; |
| 331 | } |
| 332 | |
| 333 | info = &iso->infos[iso->first_packet]; |
| 334 | |
| 335 | /* check for bogus offset/length */ |
| 336 | if (hpsb_iso_check_offset_len(iso, offset, len, &info->offset, &info->len)) |
| 337 | return -EFAULT; |
| 338 | |
| 339 | info->tag = tag; |
| 340 | info->sy = sy; |
| 341 | |
| 342 | spin_lock_irqsave(&iso->lock, flags); |
| 343 | |
| 344 | rv = iso->host->driver->isoctl(iso, XMIT_QUEUE, (unsigned long) info); |
| 345 | if (rv) |
| 346 | goto out; |
| 347 | |
| 348 | /* increment cursors */ |
| 349 | iso->first_packet = (iso->first_packet+1) % iso->buf_packets; |
| 350 | iso->xmit_cycle = (iso->xmit_cycle+1) % 8000; |
| 351 | iso->n_ready_packets--; |
| 352 | |
| 353 | if (iso->prebuffer != 0) { |
| 354 | iso->prebuffer--; |
| 355 | if (iso->prebuffer <= 0) { |
| 356 | iso->prebuffer = 0; |
| 357 | rv = do_iso_xmit_start(iso, iso->start_cycle); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | out: |
| 362 | spin_unlock_irqrestore(&iso->lock, flags); |
| 363 | return rv; |
| 364 | } |
| 365 | |
| 366 | int hpsb_iso_xmit_sync(struct hpsb_iso *iso) |
| 367 | { |
| 368 | if (iso->type != HPSB_ISO_XMIT) |
| 369 | return -EINVAL; |
| 370 | |
| 371 | return wait_event_interruptible(iso->waitq, hpsb_iso_n_ready(iso) == iso->buf_packets); |
| 372 | } |
| 373 | |
| 374 | void hpsb_iso_packet_sent(struct hpsb_iso *iso, int cycle, int error) |
| 375 | { |
| 376 | unsigned long flags; |
| 377 | spin_lock_irqsave(&iso->lock, flags); |
| 378 | |
| 379 | /* predict the cycle of the next packet to be queued */ |
| 380 | |
| 381 | /* jump ahead by the number of packets that are already buffered */ |
| 382 | cycle += iso->buf_packets - iso->n_ready_packets; |
| 383 | cycle %= 8000; |
| 384 | |
| 385 | iso->xmit_cycle = cycle; |
| 386 | iso->n_ready_packets++; |
| 387 | iso->pkt_dma = (iso->pkt_dma + 1) % iso->buf_packets; |
| 388 | |
| 389 | if (iso->n_ready_packets == iso->buf_packets || error != 0) { |
| 390 | /* the buffer has run empty! */ |
| 391 | atomic_inc(&iso->overflows); |
| 392 | } |
| 393 | |
| 394 | spin_unlock_irqrestore(&iso->lock, flags); |
| 395 | } |
| 396 | |
| 397 | void hpsb_iso_packet_received(struct hpsb_iso *iso, u32 offset, u16 len, |
| 398 | u16 cycle, u8 channel, u8 tag, u8 sy) |
| 399 | { |
| 400 | unsigned long flags; |
| 401 | spin_lock_irqsave(&iso->lock, flags); |
| 402 | |
| 403 | if (iso->n_ready_packets == iso->buf_packets) { |
| 404 | /* overflow! */ |
| 405 | atomic_inc(&iso->overflows); |
| 406 | } else { |
| 407 | struct hpsb_iso_packet_info *info = &iso->infos[iso->pkt_dma]; |
| 408 | info->offset = offset; |
| 409 | info->len = len; |
| 410 | info->cycle = cycle; |
| 411 | info->channel = channel; |
| 412 | info->tag = tag; |
| 413 | info->sy = sy; |
| 414 | |
| 415 | iso->pkt_dma = (iso->pkt_dma+1) % iso->buf_packets; |
| 416 | iso->n_ready_packets++; |
| 417 | } |
| 418 | |
| 419 | spin_unlock_irqrestore(&iso->lock, flags); |
| 420 | } |
| 421 | |
| 422 | int hpsb_iso_recv_release_packets(struct hpsb_iso *iso, unsigned int n_packets) |
| 423 | { |
| 424 | unsigned long flags; |
| 425 | unsigned int i; |
| 426 | int rv = 0; |
| 427 | |
| 428 | if (iso->type != HPSB_ISO_RECV) |
| 429 | return -1; |
| 430 | |
| 431 | spin_lock_irqsave(&iso->lock, flags); |
| 432 | for (i = 0; i < n_packets; i++) { |
| 433 | rv = iso->host->driver->isoctl(iso, RECV_RELEASE, |
| 434 | (unsigned long) &iso->infos[iso->first_packet]); |
| 435 | if (rv) |
| 436 | break; |
| 437 | |
| 438 | iso->first_packet = (iso->first_packet+1) % iso->buf_packets; |
| 439 | iso->n_ready_packets--; |
| 440 | } |
| 441 | spin_unlock_irqrestore(&iso->lock, flags); |
| 442 | return rv; |
| 443 | } |
| 444 | |
| 445 | void hpsb_iso_wake(struct hpsb_iso *iso) |
| 446 | { |
| 447 | wake_up_interruptible(&iso->waitq); |
| 448 | |
| 449 | if (iso->callback) |
| 450 | iso->callback(iso); |
| 451 | } |