24 #include <linux/device.h>
26 #include <linux/errno.h>
32 #include <linux/kernel.h>
35 #include <linux/module.h>
37 #include <linux/poll.h>
38 #include <linux/sched.h>
39 #include <linux/slab.h>
41 #include <linux/string.h>
42 #include <linux/time.h>
45 #include <linux/wait.h>
54 #define FW_CDEV_KERNEL_VERSION 5
55 #define FW_CDEV_VERSION_EVENT_REQUEST2 4
56 #define FW_CDEV_VERSION_ALLOCATE_REGION_END 4
85 kref_get(&client->
kref);
88 static void client_release(
struct kref *
kref)
92 fw_device_put(client->
device);
98 kref_put(&client->
kref, client_release);
159 if (resource->
release == release_iso_resource)
224 return compat_ptr(value);
226 return (
void __user *)(
unsigned long)value;
229 static u64 uptr_to_u64(
void __user *
ptr)
232 return ptr_to_compat(ptr);
234 return (
u64)(
unsigned long)ptr;
237 static inline void __user *u64_to_uptr(
u64 value)
239 return (
void __user *)(
unsigned long)value;
242 static inline u64 uptr_to_u64(
void __user *ptr)
244 return (
u64)(
unsigned long)ptr;
257 if (fw_device_is_shutdown(device)) {
258 fw_device_put(device);
262 client = kzalloc(
sizeof(*client),
GFP_KERNEL);
263 if (client ==
NULL) {
264 fw_device_put(device);
275 INIT_LIST_HEAD(&client->
link);
276 kref_init(&client->
kref);
283 static void queue_event(
struct client *client,
struct event *
event,
284 void *
data0,
size_t size0,
void *
data1,
size_t size1)
288 event->v[0].data =
data0;
289 event->v[0].size = size0;
290 event->v[1].data =
data1;
291 event->v[1].size = size1;
298 spin_unlock_irqrestore(&client->
lock, flags);
303 static int dequeue_event(
struct client *client,
312 fw_device_is_shutdown(client->
device));
317 fw_device_is_shutdown(client->
device))
320 spin_lock_irq(&client->
lock);
323 spin_unlock_irq(&client->
lock);
327 size =
min(event->
v[i].size, count - total);
342 static ssize_t fw_device_op_read(
struct file *file,
char __user *buffer,
343 size_t count, loff_t *
offset)
347 return dequeue_event(client, buffer, count);
351 struct client *client)
355 spin_lock_irq(&card->
lock);
359 event->generation = client->
device->generation;
360 event->node_id = client->
device->node_id;
361 event->local_node_id = card->
local_node->node_id;
363 event->irm_node_id = card->
irm_node->node_id;
364 event->root_node_id = card->
root_node->node_id;
366 spin_unlock_irq(&card->
lock);
369 static void for_each_client(
struct fw_device *device,
370 void (*
callback)(
struct client *client))
380 static
int schedule_reallocations(
int id,
void *
p,
void *
data)
382 schedule_if_iso_resource(p);
387 static void queue_bus_reset_event(
struct client *client)
393 fw_notice(client->
device->card,
"out of memory when allocating event\n");
397 fill_bus_reset_event(&e->
reset, client);
399 queue_event(client, &e->
event,
402 spin_lock_irq(&client->
lock);
404 spin_unlock_irq(&client->
lock);
409 for_each_client(device, queue_bus_reset_event);
412 static void wake_up_client(
struct client *client)
419 for_each_client(device, wake_up_client);
445 static int ioctl_get_info(
struct client *client,
union ioctl_arg *
arg)
449 unsigned long ret = 0;
459 size_t have = client->
device->config_rom_length * 4;
462 client->
device->config_rom,
min(want, have));
475 fill_bus_reset_event(&bus_reset, client);
479 if (ret == 0 && list_empty(&client->
link))
487 static int add_client_resource(
struct client *client,
505 schedule_if_iso_resource(resource);
507 spin_unlock_irqrestore(&client->
lock, flags);
512 return ret < 0 ? ret : 0;
515 static int release_client_resource(
struct client *client,
u32 handle,
521 spin_lock_irq(&client->
lock);
526 if (resource && resource->
release == release)
528 spin_unlock_irq(&client->
lock);
530 if (!(resource && resource->
release == release))
536 resource->
release(client, resource);
543 static void release_transaction(
struct client *client,
548 static void complete_transaction(
struct fw_card *
card,
int rcode,
553 struct client *client = e->
client;
556 if (length < rsp->length)
565 spin_unlock_irqrestore(&client->
lock, flags);
578 queue_event(client, &e->
event, rsp,
sizeof(*rsp),
581 queue_event(client, &e->
event, rsp,
sizeof(*rsp) + rsp->
length,
588 static int init_request(
struct client *client,
590 int destination_id,
int speed)
596 (request->
length > 4096 || request->
length > 512 << speed))
613 u64_to_uptr(request->
data), request->
length)) {
618 e->
r.resource.release = release_transaction;
619 ret = add_client_resource(client, &e->
r.resource,
GFP_KERNEL);
626 request->
length, complete_transaction, e);
635 static int ioctl_send_request(
struct client *client,
union ioctl_arg *arg)
655 client->
device->max_speed);
658 static inline bool is_fcp_request(
struct fw_request *request)
660 return request ==
NULL;
663 static void release_request(
struct client *client,
669 if (is_fcp_request(r->
request))
674 fw_card_put(r->
card);
679 int tcode,
int destination,
int source,
681 void *payload,
size_t length,
void *callback_data)
687 void *fcp_frame =
NULL;
696 fw_notice(card,
"out of memory when allocating event\n");
704 if (is_fcp_request(request)) {
710 if (fcp_frame ==
NULL)
716 r->
resource.release = release_request;
733 event_size0 =
sizeof(*req);
747 event_size0 =
sizeof(*req);
751 &e->
req, event_size0, r->
data, length);
759 if (!is_fcp_request(request))
765 static void release_address_handler(
struct client *client,
775 static int ioctl_allocate(
struct client *client,
union ioctl_arg *arg)
793 r->
handler.address_callback = handle_request;
805 r->
resource.release = release_address_handler;
808 release_address_handler(client, &r->
resource);
816 static int ioctl_deallocate(
struct client *client,
union ioctl_arg *arg)
818 return release_client_resource(client, arg->
deallocate.handle,
819 release_address_handler,
NULL);
822 static int ioctl_send_response(
struct client *client,
union ioctl_arg *arg)
829 if (release_client_resource(client, a->
handle,
830 release_request, &resource) < 0)
835 if (is_fcp_request(r->
request))
850 fw_card_put(r->
card);
856 static int ioctl_initiate_bus_reset(
struct client *client,
union ioctl_arg *arg)
863 static void release_descriptor(
struct client *client,
873 static int ioctl_add_descriptor(
struct client *client,
union ioctl_arg *arg)
880 if (!client->
device->is_local)
904 r->
resource.release = release_descriptor;
919 static int ioctl_remove_descriptor(
struct client *client,
union ioctl_arg *arg)
922 release_descriptor,
NULL);
928 struct client *client =
data;
933 fw_notice(context->
card,
"out of memory when allocating event\n");
948 struct client *client =
data;
953 fw_notice(context->
card,
"out of memory when allocating event\n");
972 static int ioctl_create_iso_context(
struct client *client,
union ioctl_arg *arg)
1010 if (IS_ERR(context))
1011 return PTR_ERR(context);
1014 spin_lock_irq(&client->
lock);
1016 spin_unlock_irq(&client->
lock);
1024 iso_dma_direction(context));
1026 spin_unlock_irq(&client->
lock);
1035 spin_unlock_irq(&client->
lock);
1042 static int ioctl_set_iso_channels(
struct client *client,
union ioctl_arg *arg)
1054 #define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
1055 #define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
1056 #define GET_SKIP(v) (((v) >> 17) & 0x01)
1057 #define GET_TAG(v) (((v) >> 18) & 0x03)
1058 #define GET_SY(v) (((v) >> 20) & 0x0f)
1059 #define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
1061 static int ioctl_queue_iso(
struct client *client,
union ioctl_arg *arg)
1066 unsigned long payload, buffer_end, transmit_header_bytes = 0;
1089 payload >= buffer_end) {
1101 end = (
void __user *)p + a->
size;
1110 u.packet.sy =
GET_SY(control);
1113 switch (ctx->
type) {
1115 if (
u.packet.header_length & 3)
1117 transmit_header_bytes =
u.packet.header_length;
1121 if (
u.packet.header_length == 0 ||
1127 if (
u.packet.payload_length == 0 ||
1128 u.packet.payload_length & 3)
1134 &p->
header[transmit_header_bytes / 4];
1138 (
u.packet.header, p->
header, transmit_header_bytes))
1141 u.packet.header_length +
u.packet.payload_length > 0)
1143 if (payload +
u.packet.payload_length > buffer_end)
1147 &client->
buffer, payload))
1151 payload +=
u.packet.payload_length;
1163 static int ioctl_start_iso(
struct client *client,
union ioctl_arg *arg)
1185 static int ioctl_stop_iso(
struct client *client,
union ioctl_arg *arg)
1195 static int ioctl_flush_iso(
struct client *client,
union ioctl_arg *arg)
1205 static int ioctl_get_cycle_timer2(
struct client *client,
union ioctl_arg *arg)
1234 static int ioctl_get_cycle_timer(
struct client *client,
union ioctl_arg *arg)
1240 ioctl_get_cycle_timer2(client, (
union ioctl_arg *)&ct2);
1253 struct client *client = r->
client;
1257 spin_lock_irq(&client->
lock);
1258 generation = client->
device->generation;
1261 if (todo == ISO_RES_ALLOC &&
1263 client->
device->card->reset_jiffies +
HZ)) {
1268 skip = todo == ISO_RES_REALLOC &&
1271 free = todo == ISO_RES_DEALLOC ||
1272 todo == ISO_RES_ALLOC_ONCE ||
1273 todo == ISO_RES_DEALLOC_ONCE;
1275 spin_unlock_irq(&client->
lock);
1284 todo == ISO_RES_ALLOC ||
1285 todo == ISO_RES_REALLOC ||
1286 todo == ISO_RES_ALLOC_ONCE);
1292 if (channel == -
EAGAIN &&
1293 (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC))
1296 success = channel >= 0 || bandwidth > 0;
1298 spin_lock_irq(&client->
lock);
1303 if (r->
todo == ISO_RES_ALLOC)
1304 r->
todo = ISO_RES_REALLOC;
1309 if (r->
todo == ISO_RES_REALLOC && !success &&
1316 spin_unlock_irq(&client->
lock);
1318 if (todo == ISO_RES_ALLOC && channel >= 0)
1321 if (todo == ISO_RES_REALLOC && success)
1324 if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) {
1335 queue_event(client, &e->
event,
1348 static void release_iso_resource(
struct client *client,
1354 spin_lock_irq(&client->
lock);
1356 schedule_iso_resource(r, 0);
1357 spin_unlock_irq(&client->
lock);
1360 static int init_iso_resource(
struct client *client,
1395 r->
resource.release = release_iso_resource;
1402 schedule_iso_resource(r, 0);
1415 static int ioctl_allocate_iso_resource(
struct client *client,
1418 return init_iso_resource(client,
1422 static int ioctl_deallocate_iso_resource(
struct client *client,
1425 return release_client_resource(client,
1429 static int ioctl_allocate_iso_resource_once(
struct client *client,
1432 return init_iso_resource(client,
1436 static int ioctl_deallocate_iso_resource_once(
struct client *client,
1439 return init_iso_resource(client,
1448 static int ioctl_get_speed(
struct client *client,
union ioctl_arg *arg)
1450 return client->
device->max_speed;
1453 static int ioctl_send_broadcast_request(
struct client *client,
1473 static int ioctl_send_stream_packet(
struct client *client,
union ioctl_arg *arg)
1479 if (a->
speed > client->
device->card->link_speed ||
1486 dest = fw_stream_packet_destination_id(a->
tag, a->
channel, a->
sy);
1488 request.length = a->
length;
1490 request.data = a->
data;
1493 return init_request(client, &request, dest, a->
speed);
1522 static int ioctl_send_phy_packet(
struct client *client,
union ioctl_arg *arg)
1529 if (!client->
device->is_local)
1541 e->
p.header[1] = a->
data[0];
1542 e->
p.header[2] = a->
data[1];
1543 e->
p.header_length = 12;
1544 e->
p.callback = outbound_phy_packet_callback;
1547 if (is_ping_packet(a->
data))
1550 card->
driver->send_request(card, &e->
p);
1555 static int ioctl_receive_phy_packets(
struct client *client,
union ioctl_arg *arg)
1561 if (!client->
device->is_local)
1564 spin_lock_irq(&card->
lock);
1569 spin_unlock_irq(&card->
lock);
1578 unsigned long flags;
1585 fw_notice(card,
"out of memory when allocating event\n");
1594 queue_event(client, &e->
event,
1598 spin_unlock_irqrestore(&card->
lock, flags);
1601 static int (*
const ioctl_handlers[])(
struct client *,
union ioctl_arg *) = {
1602 [0x00] = ioctl_get_info,
1603 [0x01] = ioctl_send_request,
1604 [0x02] = ioctl_allocate,
1605 [0x03] = ioctl_deallocate,
1606 [0x04] = ioctl_send_response,
1607 [0x05] = ioctl_initiate_bus_reset,
1608 [0x06] = ioctl_add_descriptor,
1609 [0x07] = ioctl_remove_descriptor,
1610 [0x08] = ioctl_create_iso_context,
1611 [0x09] = ioctl_queue_iso,
1612 [0x0a] = ioctl_start_iso,
1613 [0x0b] = ioctl_stop_iso,
1614 [0x0c] = ioctl_get_cycle_timer,
1615 [0x0d] = ioctl_allocate_iso_resource,
1616 [0x0e] = ioctl_deallocate_iso_resource,
1617 [0x0f] = ioctl_allocate_iso_resource_once,
1618 [0x10] = ioctl_deallocate_iso_resource_once,
1619 [0x11] = ioctl_get_speed,
1620 [0x12] = ioctl_send_broadcast_request,
1621 [0x13] = ioctl_send_stream_packet,
1622 [0x14] = ioctl_get_cycle_timer2,
1623 [0x15] = ioctl_send_phy_packet,
1624 [0x16] = ioctl_receive_phy_packets,
1625 [0x17] = ioctl_set_iso_channels,
1626 [0x18] = ioctl_flush_iso,
1629 static int dispatch_ioctl(
struct client *client,
1630 unsigned int cmd,
void __user *arg)
1635 if (fw_device_is_shutdown(client->
device))
1661 static long fw_device_op_ioctl(
struct file *file,
1662 unsigned int cmd,
unsigned long arg)
1664 return dispatch_ioctl(file->
private_data, cmd, (
void __user *)arg);
1667 #ifdef CONFIG_COMPAT
1668 static long fw_device_op_compat_ioctl(
struct file *file,
1669 unsigned int cmd,
unsigned long arg)
1671 return dispatch_ioctl(file->
private_data, cmd, compat_ptr(arg));
1675 static int fw_device_op_mmap(
struct file *file,
struct vm_area_struct *vma)
1679 int page_count,
ret;
1681 if (fw_device_is_shutdown(client->
device))
1704 spin_lock_irq(&client->
lock);
1711 spin_unlock_irq(&client->
lock);
1725 static int is_outbound_transaction_resource(
int id,
void *
p,
void *data)
1729 return resource->
release == release_transaction;
1732 static int has_outbound_transactions(
struct client *client)
1736 spin_lock_irq(&client->
lock);
1738 is_outbound_transaction_resource,
NULL);
1739 spin_unlock_irq(&client->
lock);
1744 static int shutdown_resource(
int id,
void *
p,
void *data)
1747 struct client *client =
data;
1749 resource->
release(client, resource);
1755 static int fw_device_op_release(
struct inode *inode,
struct file *file)
1758 struct event *
event, *next_event;
1760 spin_lock_irq(&client->
device->card->lock);
1762 spin_unlock_irq(&client->
device->card->lock);
1771 if (client->
buffer.pages)
1775 spin_lock_irq(&client->
lock);
1777 spin_unlock_irq(&client->
lock);
1795 struct client *client = file->private_data;
1796 unsigned int mask = 0;
1798 poll_wait(file, &client->
wait, pt);
1800 if (fw_device_is_shutdown(client->
device))
1811 .open = fw_device_op_open,
1812 .read = fw_device_op_read,
1813 .unlocked_ioctl = fw_device_op_ioctl,
1814 .mmap = fw_device_op_mmap,
1815 .release = fw_device_op_release,
1816 .poll = fw_device_op_poll,
1817 #ifdef CONFIG_COMPAT
1818 .compat_ioctl = fw_device_op_compat_ioctl,