4 * Core support: hpsb_packet management, packet handling and forwarding to
5 * highlevel or lowlevel code
7 * Copyright (C) 1999, 2000 Andreas E. Bombe
8 * 2002 Manfred Weihs <weihs@ict.tuwien.ac.at>
10 * This code is licensed under the GPL. See the file COPYING in the root
11 * directory of the kernel sources for details.
16 * Manfred Weihs <weihs@ict.tuwien.ac.at>
17 * loopback functionality in hpsb_send_packet
18 * allow highlevel drivers to disable automatic response generation
19 * and to generate responses themselves (deferred)
23 #include <linux/config.h>
24 #include <linux/kernel.h>
25 #include <linux/list.h>
26 #include <linux/string.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/interrupt.h>
30 #include <linux/module.h>
31 #include <linux/proc_fs.h>
32 #include <asm/bitops.h>
33 #include <asm/byteorder.h>
34 #include <asm/semaphore.h>
36 #include "ieee1394_types.h"
39 #include "ieee1394_core.h"
40 #include "highlevel.h"
41 #include "ieee1394_transactions.h"
44 #include "ieee1394_hotplug.h"
47 * Disable the nodemgr detection and config rom reading functionality.
49 MODULE_PARM(disable_nodemgr, "i");
50 MODULE_PARM_DESC(disable_nodemgr, "Disable nodemgr functionality.");
51 static int disable_nodemgr = 0;
53 MODULE_PARM(disable_hotplug, "i");
54 MODULE_PARM_DESC(disable_hotplug, "Disable hotplug for detected nodes.");
55 static int disable_hotplug = 0;
57 /* We are GPL, so treat us special */
58 MODULE_LICENSE("GPL");
60 static kmem_cache_t *hpsb_packet_cache;
62 /* Some globals used */
63 const char *hpsb_speedto_str[] = { "S100", "S200", "S400" };
65 static void dump_packet(const char *text, quadlet_t *data, int size)
70 size = (size > 4 ? 4 : size);
72 printk(KERN_DEBUG "ieee1394: %s", text);
73 for (i = 0; i < size; i++) {
74 printk(" %8.8x", data[i]);
79 static void process_complete_tasks(struct hpsb_packet *packet)
81 struct list_head *lh, *next;
83 list_for_each_safe(lh, next, &packet->complete_tq) {
84 struct hpsb_queue_struct *tq =
85 list_entry(lh, struct hpsb_queue_struct, hpsb_queue_list);
86 list_del(&tq->hpsb_queue_list);
87 hpsb_schedule_work(tq);
94 * hpsb_add_packet_complete_task - add a new task for when a packet completes
95 * @packet: the packet whose completion we want the task added to
96 * @tq: the hpsb_queue_struct describing the task to add
98 void hpsb_add_packet_complete_task(struct hpsb_packet *packet, struct hpsb_queue_struct *tq)
100 list_add_tail(&tq->hpsb_queue_list, &packet->complete_tq);
105 * alloc_hpsb_packet - allocate new packet structure
106 * @data_size: size of the data block to be allocated
108 * This function allocates, initializes and returns a new &struct hpsb_packet.
109 * It can be used in interrupt context. A header block is always included, its
110 * size is big enough to contain all possible 1394 headers. The data block is
111 * only allocated when @data_size is not zero.
113 * For packets for which responses will be received the @data_size has to be big
114 * enough to contain the response's data block since no further allocation
115 * occurs at response matching time.
117 * The packet's generation value will be set to the current generation number
118 * for ease of use. Remember to overwrite it with your own recorded generation
119 * number if you can not be sure that your code will not race with a bus reset.
121 * Return value: A pointer to a &struct hpsb_packet or NULL on allocation
124 struct hpsb_packet *alloc_hpsb_packet(size_t data_size)
126 struct hpsb_packet *packet = NULL;
128 int kmflags = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
130 packet = kmem_cache_alloc(hpsb_packet_cache, kmflags);
134 memset(packet, 0, sizeof(struct hpsb_packet));
135 packet->header = packet->embedded_header;
138 data = kmalloc(data_size + 8, kmflags);
140 kmem_cache_free(hpsb_packet_cache, packet);
145 packet->data_size = data_size;
148 INIT_LIST_HEAD(&packet->complete_tq);
149 INIT_LIST_HEAD(&packet->list);
150 sema_init(&packet->state_change, 0);
151 packet->state = hpsb_unused;
152 packet->generation = -1;
160 * free_hpsb_packet - free packet and data associated with it
161 * @packet: packet to free (is NULL safe)
163 * This function will free packet->data, packet->header and finally the packet
166 void free_hpsb_packet(struct hpsb_packet *packet)
171 kmem_cache_free(hpsb_packet_cache, packet);
175 int hpsb_reset_bus(struct hpsb_host *host, int type)
177 if (!host->in_bus_reset) {
178 host->driver->devctl(host, RESET_BUS, type);
186 int hpsb_bus_reset(struct hpsb_host *host)
188 if (host->in_bus_reset) {
189 HPSB_NOTICE("%s called while bus reset already in progress",
194 abort_requests(host);
195 host->in_bus_reset = 1;
198 host->busmgr_id = -1;
201 host->node_count = 0;
202 host->selfid_count = 0;
209 * Verify num_of_selfids SelfIDs and return number of nodes. Return zero in
210 * case verification failed.
212 static int check_selfids(struct hpsb_host *host)
215 int rest_of_selfids = host->selfid_count;
216 struct selfid *sid = (struct selfid *)host->topology_map;
217 struct ext_selfid *esid;
220 host->nodes_active = 0;
222 while (rest_of_selfids--) {
223 if (!sid->extended) {
227 if (sid->phy_id != nodeid) {
228 HPSB_INFO("SelfIDs failed monotony check with "
233 if (sid->link_active) {
234 host->nodes_active++;
236 host->irm_id = LOCAL_BUS | sid->phy_id;
239 esid = (struct ext_selfid *)sid;
241 if ((esid->phy_id != nodeid)
242 || (esid->seq_nr != esid_seq)) {
243 HPSB_INFO("SelfIDs failed monotony check with "
244 "%d/%d", esid->phy_id, esid->seq_nr);
252 esid = (struct ext_selfid *)(sid - 1);
253 while (esid->extended) {
254 if ((esid->porta == 0x2) || (esid->portb == 0x2)
255 || (esid->portc == 0x2) || (esid->portd == 0x2)
256 || (esid->porte == 0x2) || (esid->portf == 0x2)
257 || (esid->portg == 0x2) || (esid->porth == 0x2)) {
258 HPSB_INFO("SelfIDs failed root check on "
265 sid = (struct selfid *)esid;
266 if ((sid->port0 == 0x2) || (sid->port1 == 0x2) || (sid->port2 == 0x2)) {
267 HPSB_INFO("SelfIDs failed root check");
271 host->node_count = nodeid + 1;
275 static void build_speed_map(struct hpsb_host *host, int nodecount)
277 char speedcap[nodecount];
278 char cldcnt[nodecount];
279 u8 *map = host->speed_map;
281 struct ext_selfid *esid;
284 for (i = 0; i < (nodecount * 64); i += 64) {
285 for (j = 0; j < nodecount; j++) {
286 map[i+j] = SPEED_400;
290 for (i = 0; i < nodecount; i++) {
294 /* find direct children count and speed */
295 for (sid = (struct selfid *)&host->topology_map[host->selfid_count-1],
297 (void *)sid >= (void *)host->topology_map; sid--) {
299 esid = (struct ext_selfid *)sid;
301 if (esid->porta == 0x3) cldcnt[n]++;
302 if (esid->portb == 0x3) cldcnt[n]++;
303 if (esid->portc == 0x3) cldcnt[n]++;
304 if (esid->portd == 0x3) cldcnt[n]++;
305 if (esid->porte == 0x3) cldcnt[n]++;
306 if (esid->portf == 0x3) cldcnt[n]++;
307 if (esid->portg == 0x3) cldcnt[n]++;
308 if (esid->porth == 0x3) cldcnt[n]++;
310 if (sid->port0 == 0x3) cldcnt[n]++;
311 if (sid->port1 == 0x3) cldcnt[n]++;
312 if (sid->port2 == 0x3) cldcnt[n]++;
314 speedcap[n] = sid->speed;
319 /* set self mapping */
320 for (i = 0; i < nodecount; i++) {
321 map[64*i + i] = speedcap[i];
324 /* fix up direct children count to total children count;
325 * also fix up speedcaps for sibling and parent communication */
326 for (i = 1; i < nodecount; i++) {
327 for (j = cldcnt[i], n = i - 1; j > 0; j--) {
328 cldcnt[i] += cldcnt[n];
329 speedcap[n] = MIN(speedcap[n], speedcap[i]);
334 for (n = 0; n < nodecount; n++) {
335 for (i = n - cldcnt[n]; i <= n; i++) {
336 for (j = 0; j < (n - cldcnt[n]); j++) {
337 map[j*64 + i] = map[i*64 + j] =
338 MIN(map[i*64 + j], speedcap[n]);
340 for (j = n + 1; j < nodecount; j++) {
341 map[j*64 + i] = map[i*64 + j] =
342 MIN(map[i*64 + j], speedcap[n]);
349 void hpsb_selfid_received(struct hpsb_host *host, quadlet_t sid)
351 if (host->in_bus_reset) {
352 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
353 HPSB_INFO("Including SelfID 0x%x", sid);
355 host->topology_map[host->selfid_count++] = sid;
357 HPSB_NOTICE("Spurious SelfID packet (0x%08x) received from bus %d",
358 sid, (host->node_id & BUS_MASK) >> 6);
362 void hpsb_selfid_complete(struct hpsb_host *host, int phyid, int isroot)
364 if (!host->in_bus_reset)
365 HPSB_NOTICE("SelfID completion called outside of bus reset!");
367 host->node_id = LOCAL_BUS | phyid;
368 host->is_root = isroot;
370 if (!check_selfids(host)) {
371 if (host->reset_retries++ < 20) {
372 /* selfid stage did not complete without error */
373 HPSB_NOTICE("Error in SelfID stage, resetting");
374 host->in_bus_reset = 0;
375 hpsb_reset_bus(host, LONG_RESET);
378 HPSB_NOTICE("Stopping out-of-control reset loop");
379 HPSB_NOTICE("Warning - topology map and speed map will not be valid");
382 build_speed_map(host, host->node_count);
385 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
386 HPSB_INFO("selfid_complete called with successful SelfID stage "
387 "... irm_id: 0x%X node_id: 0x%X",host->irm_id,host->node_id);
389 /* irm_id is kept up to date by check_selfids() */
390 if (host->irm_id == host->node_id) {
393 host->busmgr_id = host->node_id;
394 host->csr.bus_manager_id = host->node_id;
400 host->reset_retries = 0;
402 host->driver->devctl(host, ACT_CYCLE_MASTER, 1);
405 atomic_inc(&host->generation);
406 host->in_bus_reset = 0;
407 highlevel_host_reset(host);
411 void hpsb_packet_sent(struct hpsb_host *host, struct hpsb_packet *packet,
416 packet->ack_code = ackcode;
418 if (packet->no_waiter) {
419 /* must not have a tlabel allocated */
420 free_hpsb_packet(packet);
424 if (ackcode != ACK_PENDING || !packet->expect_response) {
425 packet->state = hpsb_complete;
426 up(&packet->state_change);
427 up(&packet->state_change);
428 process_complete_tasks(packet);
432 packet->state = hpsb_pending;
433 packet->sendtime = jiffies;
435 spin_lock_irqsave(&host->pending_pkt_lock, flags);
436 list_add_tail(&packet->list, &host->pending_packets);
437 spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
439 up(&packet->state_change);
440 hpsb_schedule_work(&host->timeout_tq);
444 * hpsb_send_packet - transmit a packet on the bus
445 * @packet: packet to send
447 * The packet is sent through the host specified in the packet->host field.
448 * Before sending, the packet's transmit speed is automatically determined using
449 * the local speed map when it is an async, non-broadcast packet.
451 * Possibilities for failure are that host is either not initialized, in bus
452 * reset, the packet's generation number doesn't match the current generation
453 * number or the host reports a transmit error.
455 * Return value: False (0) on failure, true (1) otherwise.
457 int hpsb_send_packet(struct hpsb_packet *packet)
459 struct hpsb_host *host = packet->host;
461 if (host->is_shutdown || host->in_bus_reset
462 || (packet->generation != get_hpsb_generation(host))) {
466 packet->state = hpsb_queued;
468 if (packet->node_id == host->node_id)
469 { /* it is a local request, so handle it locally */
471 size_t size=packet->data_size+packet->header_size;
473 int kmflags = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
474 data = kmalloc(packet->header_size + packet->data_size, kmflags);
476 HPSB_ERR("unable to allocate memory for concatenating header and data");
480 memcpy(data, packet->header, packet->header_size);
482 if (packet->data_size)
484 if (packet->data_be) {
485 memcpy(((u8*)data)+packet->header_size, packet->data, packet->data_size);
488 quadlet_t *my_data=(quadlet_t*) ((u8*) data + packet->data_size);
489 for (i=0; i < packet->data_size/4; i++) {
490 my_data[i] = cpu_to_be32(packet->data[i]);
495 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
496 dump_packet("send packet local:", packet->header,
497 packet->header_size);
499 hpsb_packet_sent(host, packet, packet->expect_response?ACK_PENDING:ACK_COMPLETE);
500 hpsb_packet_received(host, data, size, 0);
507 if (packet->type == hpsb_async && packet->node_id != ALL_NODES) {
509 host->speed_map[(host->node_id & NODE_MASK) * 64
510 + (packet->node_id & NODE_MASK)];
513 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
514 switch (packet->speed_code) {
516 dump_packet("send packet 400:", packet->header,
517 packet->header_size);
520 dump_packet("send packet 200:", packet->header,
521 packet->header_size);
524 dump_packet("send packet 100:", packet->header,
525 packet->header_size);
529 return host->driver->transmit_packet(host, packet);
532 static void send_packet_nocare(struct hpsb_packet *packet)
534 if (!hpsb_send_packet(packet)) {
535 free_hpsb_packet(packet);
540 void handle_packet_response(struct hpsb_host *host, int tcode, quadlet_t *data,
543 struct hpsb_packet *packet = NULL;
544 struct list_head *lh;
549 tlabel = (data[0] >> 10) & 0x3f;
551 spin_lock_irqsave(&host->pending_pkt_lock, flags);
553 list_for_each(lh, &host->pending_packets) {
554 packet = list_entry(lh, struct hpsb_packet, list);
555 if ((packet->tlabel == tlabel)
556 && (packet->node_id == (data[1] >> 16))){
561 if (lh == &host->pending_packets) {
562 HPSB_DEBUG("unsolicited response packet received - np");
563 dump_packet("contents:", data, 16);
564 spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
568 switch (packet->tcode) {
571 if (tcode == TCODE_WRITE_RESPONSE) tcode_match = 1;
574 if (tcode == TCODE_READQ_RESPONSE) tcode_match = 1;
577 if (tcode == TCODE_READB_RESPONSE) tcode_match = 1;
579 case TCODE_LOCK_REQUEST:
580 if (tcode == TCODE_LOCK_RESPONSE) tcode_match = 1;
584 if (!tcode_match || (packet->tlabel != tlabel)
585 || (packet->node_id != (data[1] >> 16))) {
586 HPSB_INFO("unsolicited response packet received");
587 dump_packet("contents:", data, 16);
589 spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
593 list_del(&packet->list);
595 spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
597 /* FIXME - update size fields? */
599 case TCODE_WRITE_RESPONSE:
600 memcpy(packet->header, data, 12);
602 case TCODE_READQ_RESPONSE:
603 memcpy(packet->header, data, 16);
605 case TCODE_READB_RESPONSE:
606 memcpy(packet->header, data, 16);
607 memcpy(packet->data, data + 4, size - 16);
609 case TCODE_LOCK_RESPONSE:
610 memcpy(packet->header, data, 16);
611 memcpy(packet->data, data + 4, (size - 16) > 8 ? 8 : size - 16);
615 packet->state = hpsb_complete;
616 up(&packet->state_change);
617 process_complete_tasks(packet);
621 static struct hpsb_packet *create_reply_packet(struct hpsb_host *host,
622 quadlet_t *data, size_t dsize)
624 struct hpsb_packet *p;
626 dsize += (dsize % 4 ? 4 - (dsize % 4) : 0);
628 p = alloc_hpsb_packet(dsize);
630 /* FIXME - send data_error response */
634 p->type = hpsb_async;
635 p->state = hpsb_unused;
637 p->node_id = data[1] >> 16;
638 p->tlabel = (data[0] >> 10) & 0x3f;
641 p->generation = get_hpsb_generation(host);
644 p->data[dsize / 4] = 0;
650 #define PREP_REPLY_PACKET(length) \
651 packet = create_reply_packet(host, data, length); \
652 if (packet == NULL) break
654 static void handle_incoming_packet(struct hpsb_host *host, int tcode,
655 quadlet_t *data, size_t size, int write_acked)
657 struct hpsb_packet *packet;
658 int length, rcode, extcode;
660 nodeid_t source = data[1] >> 16;
661 nodeid_t dest = data[0] >> 16;
662 u16 flags = (u16) data[0];
665 /* big FIXME - no error checking is done for an out of bounds length */
669 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
670 rcode = highlevel_write(host, source, dest, data+3,
674 && (((data[0] >> 16) & NODE_MASK) != NODE_MASK)
676 /* not a broadcast write, reply */
677 PREP_REPLY_PACKET(0);
678 fill_async_write_resp(packet, rcode);
679 send_packet_nocare(packet);
684 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
685 rcode = highlevel_write(host, source, dest, data+4,
686 addr, data[3]>>16, flags);
689 && (((data[0] >> 16) & NODE_MASK) != NODE_MASK)
691 /* not a broadcast write, reply */
692 PREP_REPLY_PACKET(0);
693 fill_async_write_resp(packet, rcode);
694 send_packet_nocare(packet);
699 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
700 rcode = highlevel_read(host, source, &buffer, addr, 4, flags);
703 PREP_REPLY_PACKET(0);
704 fill_async_readquad_resp(packet, rcode, buffer);
705 send_packet_nocare(packet);
710 length = data[3] >> 16;
711 PREP_REPLY_PACKET(length);
713 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
714 rcode = highlevel_read(host, source, packet->data, addr,
718 fill_async_readblock_resp(packet, rcode, length);
719 send_packet_nocare(packet);
723 case TCODE_LOCK_REQUEST:
724 length = data[3] >> 16;
725 extcode = data[3] & 0xffff;
726 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
728 PREP_REPLY_PACKET(8);
730 if ((extcode == 0) || (extcode >= 7)) {
731 /* let switch default handle error */
737 rcode = highlevel_lock(host, source, packet->data, addr,
738 data[4], 0, extcode,flags);
739 fill_async_lock_resp(packet, rcode, extcode, 4);
742 if ((extcode != EXTCODE_FETCH_ADD)
743 && (extcode != EXTCODE_LITTLE_ADD)) {
744 rcode = highlevel_lock(host, source,
748 fill_async_lock_resp(packet, rcode, extcode, 4);
750 rcode = highlevel_lock64(host, source,
751 (octlet_t *)packet->data, addr,
752 *(octlet_t *)(data + 4), 0ULL,
754 fill_async_lock_resp(packet, rcode, extcode, 8);
758 rcode = highlevel_lock64(host, source,
759 (octlet_t *)packet->data, addr,
760 *(octlet_t *)(data + 6),
761 *(octlet_t *)(data + 4),
763 fill_async_lock_resp(packet, rcode, extcode, 8);
766 rcode = RCODE_TYPE_ERROR;
767 fill_async_lock_resp(packet, rcode,
772 send_packet_nocare(packet);
774 free_hpsb_packet(packet);
780 #undef PREP_REPLY_PACKET
783 void hpsb_packet_received(struct hpsb_host *host, quadlet_t *data, size_t size,
788 if (host->in_bus_reset) {
789 HPSB_INFO("received packet during reset; ignoring");
793 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
794 dump_packet("received packet:", data, size);
797 tcode = (data[0] >> 4) & 0xf;
800 case TCODE_WRITE_RESPONSE:
801 case TCODE_READQ_RESPONSE:
802 case TCODE_READB_RESPONSE:
803 case TCODE_LOCK_RESPONSE:
804 handle_packet_response(host, tcode, data, size);
811 case TCODE_LOCK_REQUEST:
812 handle_incoming_packet(host, tcode, data, size, write_acked);
817 highlevel_iso_receive(host, data, size);
820 case TCODE_CYCLE_START:
821 /* simply ignore this packet if it is passed on */
825 HPSB_NOTICE("received packet with bogus transaction code %d",
832 void abort_requests(struct hpsb_host *host)
835 struct hpsb_packet *packet;
836 struct list_head *lh;
839 host->driver->devctl(host, CANCEL_REQUESTS, 0);
841 spin_lock_irqsave(&host->pending_pkt_lock, flags);
842 list_splice(&host->pending_packets, &llist);
843 INIT_LIST_HEAD(&host->pending_packets);
844 spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
846 list_for_each(lh, &llist) {
847 packet = list_entry(lh, struct hpsb_packet, list);
848 packet->state = hpsb_complete;
849 packet->ack_code = ACKX_ABORTED;
850 up(&packet->state_change);
851 process_complete_tasks(packet);
855 void abort_timedouts(struct hpsb_host *host)
858 struct hpsb_packet *packet;
859 unsigned long expire;
860 struct list_head *lh, *next;
861 LIST_HEAD(expiredlist);
863 spin_lock_irqsave(&host->csr.lock, flags);
864 expire = (host->csr.split_timeout_hi * 8000
865 + (host->csr.split_timeout_lo >> 19))
867 /* Avoid shortening of timeout due to rounding errors: */
869 spin_unlock_irqrestore(&host->csr.lock, flags);
872 spin_lock_irqsave(&host->pending_pkt_lock, flags);
874 for (lh = host->pending_packets.next; lh != &host->pending_packets; lh = next) {
875 packet = list_entry(lh, struct hpsb_packet, list);
877 if (time_before(packet->sendtime + expire, jiffies)) {
878 list_del(&packet->list);
879 list_add(&packet->list, &expiredlist);
883 if (!list_empty(&host->pending_packets))
884 hpsb_schedule_work(&host->timeout_tq);
886 spin_unlock_irqrestore(&host->pending_pkt_lock, flags);
888 list_for_each(lh, &expiredlist) {
889 packet = list_entry(lh, struct hpsb_packet, list);
890 packet->state = hpsb_complete;
891 packet->ack_code = ACKX_TIMEOUT;
892 up(&packet->state_change);
893 process_complete_tasks(packet);
899 * character device dispatching (see ieee1394_core.h)
900 * Dan Maas <dmaas@dcine.com>
904 struct file_operations *file_ops;
905 struct module *module;
906 } ieee1394_chardevs[16];
908 static rwlock_t ieee1394_chardevs_lock = RW_LOCK_UNLOCKED;
910 static int ieee1394_dispatch_open(struct inode *inode, struct file *file);
912 static struct file_operations ieee1394_chardev_ops = {
914 .open = ieee1394_dispatch_open,
917 devfs_handle_t ieee1394_devfs_handle;
920 /* claim a block of minor numbers */
921 int ieee1394_register_chardev(int blocknum,
922 struct module *module,
923 struct file_operations *file_ops)
927 if( (blocknum < 0) || (blocknum > 15) )
930 write_lock(&ieee1394_chardevs_lock);
932 if(ieee1394_chardevs[blocknum].file_ops == NULL) {
933 /* grab the minor block */
934 ieee1394_chardevs[blocknum].file_ops = file_ops;
935 ieee1394_chardevs[blocknum].module = module;
939 /* block already taken */
943 write_unlock(&ieee1394_chardevs_lock);
948 /* release a block of minor numbers */
949 void ieee1394_unregister_chardev(int blocknum)
951 if( (blocknum < 0) || (blocknum > 15) )
954 write_lock(&ieee1394_chardevs_lock);
956 if(ieee1394_chardevs[blocknum].file_ops) {
957 ieee1394_chardevs[blocknum].file_ops = NULL;
958 ieee1394_chardevs[blocknum].module = NULL;
961 write_unlock(&ieee1394_chardevs_lock);
965 ieee1394_get_chardev() - look up and acquire a character device
966 driver that has previously registered using ieee1394_register_chardev()
968 On success, returns 1 and sets module and file_ops to the driver.
969 The module will have an incremented reference count.
971 On failure, returns 0.
972 The module will NOT have an incremented reference count.
975 static int ieee1394_get_chardev(int blocknum,
976 struct module **module,
977 struct file_operations **file_ops)
981 if( (blocknum < 0) || (blocknum > 15) )
984 read_lock(&ieee1394_chardevs_lock);
986 *module = ieee1394_chardevs[blocknum].module;
987 *file_ops = ieee1394_chardevs[blocknum].file_ops;
989 if(*file_ops == NULL)
992 /* don't need try_inc_mod_count if the driver is non-modular */
993 if(*module && (try_inc_mod_count(*module) == 0))
1000 read_unlock(&ieee1394_chardevs_lock);
1004 /* the point of entry for open() on any ieee1394 character device */
1005 static int ieee1394_dispatch_open(struct inode *inode, struct file *file)
1007 struct file_operations *file_ops;
1008 struct module *module;
1013 Maintaining correct module reference counts is tricky here!
1015 The key thing to remember is that the VFS increments the
1016 reference count of ieee1394 before it calls
1017 ieee1394_dispatch_open().
1019 If the open() succeeds, then we need to transfer this extra
1020 reference to the task-specific driver module (e.g. raw1394).
1021 The VFS will deref the driver module automatically when the
1022 file is later released.
1024 If the open() fails, then the VFS will drop the
1025 reference count of whatever module file->f_op->owner points
1026 to, immediately after this function returns.
1029 /* shift away lower four bits of the minor
1030 to get the index of the ieee1394_driver
1033 blocknum = (minor(inode->i_rdev) >> 4) & 0xF;
1035 /* look up the driver */
1037 if(ieee1394_get_chardev(blocknum, &module, &file_ops) == 0)
1040 /* redirect all subsequent requests to the driver's
1041 own file_operations */
1042 file->f_op = file_ops;
1044 /* at this point BOTH ieee1394 and the task-specific driver have
1045 an extra reference */
1047 /* follow through with the open() */
1048 retval = file_ops->open(inode, file);
1052 /* If the open() succeeded, then ieee1394 will be left
1053 with an extra module reference, so we discard it here.
1055 The task-specific driver still has the extra
1056 reference given to it by ieee1394_get_chardev().
1057 This extra reference prevents the module from
1058 unloading while the file is open, and will be
1059 dropped by the VFS when the file is released.
1063 __MOD_DEC_USE_COUNT((struct module*) THIS_MODULE);
1065 /* note that if ieee1394 is compiled into the kernel,
1066 THIS_MODULE will be (void*) NULL, hence the if and
1067 the cast are necessary */
1071 /* if the open() failed, then we need to drop the
1072 extra reference we gave to the task-specific
1076 __MOD_DEC_USE_COUNT(module);
1078 /* point the file's f_ops back to ieee1394. The VFS will then
1079 decrement ieee1394's reference count immediately after this
1080 function returns. */
1082 file->f_op = &ieee1394_chardev_ops;
1088 struct proc_dir_entry *ieee1394_procfs_entry;
1090 static int __init ieee1394_init(void)
1092 hpsb_packet_cache = kmem_cache_create("hpsb_packet", sizeof(struct hpsb_packet),
1095 ieee1394_devfs_handle = devfs_mk_dir(NULL, "ieee1394", NULL);
1097 if (register_chrdev(IEEE1394_MAJOR, "ieee1394", &ieee1394_chardev_ops)) {
1098 HPSB_ERR("unable to register character device major %d!\n", IEEE1394_MAJOR);
1099 devfs_unregister(ieee1394_devfs_handle);
1103 #ifdef CONFIG_PROC_FS
1104 /* Must be done before we start everything else, since the drivers
1106 ieee1394_procfs_entry = proc_mkdir( "ieee1394", proc_bus);
1107 if (ieee1394_procfs_entry == NULL) {
1108 HPSB_ERR("unable to create /proc/bus/ieee1394\n");
1109 unregister_chrdev(IEEE1394_MAJOR, "ieee1394");
1110 devfs_unregister(ieee1394_devfs_handle);
1113 ieee1394_procfs_entry->owner = THIS_MODULE;
1116 init_hpsb_highlevel();
1118 if (!disable_nodemgr)
1119 init_ieee1394_nodemgr(disable_hotplug);
1121 HPSB_INFO("nodemgr functionality disabled");
1126 static void __exit ieee1394_cleanup(void)
1128 if (!disable_nodemgr)
1129 cleanup_ieee1394_nodemgr();
1132 kmem_cache_destroy(hpsb_packet_cache);
1134 unregister_chrdev(IEEE1394_MAJOR, "ieee1394");
1136 /* it's ok to pass a NULL devfs_handle to devfs_unregister */
1137 devfs_unregister(ieee1394_devfs_handle);
1139 remove_proc_entry("ieee1394", proc_bus);
1142 module_init(ieee1394_init);
1143 module_exit(ieee1394_cleanup);
1145 /* Exported symbols */
1146 EXPORT_SYMBOL(hpsb_alloc_host);
1147 EXPORT_SYMBOL(hpsb_add_host);
1148 EXPORT_SYMBOL(hpsb_remove_host);
1149 EXPORT_SYMBOL(hpsb_ref_host);
1150 EXPORT_SYMBOL(hpsb_unref_host);
1151 EXPORT_SYMBOL(hpsb_speedto_str);
1152 EXPORT_SYMBOL(hpsb_add_packet_complete_task);
1154 EXPORT_SYMBOL(alloc_hpsb_packet);
1155 EXPORT_SYMBOL(free_hpsb_packet);
1156 EXPORT_SYMBOL(hpsb_send_packet);
1157 EXPORT_SYMBOL(hpsb_reset_bus);
1158 EXPORT_SYMBOL(hpsb_bus_reset);
1159 EXPORT_SYMBOL(hpsb_selfid_received);
1160 EXPORT_SYMBOL(hpsb_selfid_complete);
1161 EXPORT_SYMBOL(hpsb_packet_sent);
1162 EXPORT_SYMBOL(hpsb_packet_received);
1164 EXPORT_SYMBOL(get_tlabel);
1165 EXPORT_SYMBOL(free_tlabel);
1166 EXPORT_SYMBOL(fill_async_readquad);
1167 EXPORT_SYMBOL(fill_async_readquad_resp);
1168 EXPORT_SYMBOL(fill_async_readblock);
1169 EXPORT_SYMBOL(fill_async_readblock_resp);
1170 EXPORT_SYMBOL(fill_async_writequad);
1171 EXPORT_SYMBOL(fill_async_writeblock);
1172 EXPORT_SYMBOL(fill_async_write_resp);
1173 EXPORT_SYMBOL(fill_async_lock);
1174 EXPORT_SYMBOL(fill_async_lock_resp);
1175 EXPORT_SYMBOL(fill_iso_packet);
1176 EXPORT_SYMBOL(fill_phy_packet);
1177 EXPORT_SYMBOL(hpsb_make_readqpacket);
1178 EXPORT_SYMBOL(hpsb_make_readbpacket);
1179 EXPORT_SYMBOL(hpsb_make_writeqpacket);
1180 EXPORT_SYMBOL(hpsb_make_writebpacket);
1181 EXPORT_SYMBOL(hpsb_make_lockpacket);
1182 EXPORT_SYMBOL(hpsb_make_lock64packet);
1183 EXPORT_SYMBOL(hpsb_make_phypacket);
1184 EXPORT_SYMBOL(hpsb_packet_success);
1185 EXPORT_SYMBOL(hpsb_make_packet);
1186 EXPORT_SYMBOL(hpsb_read);
1187 EXPORT_SYMBOL(hpsb_write);
1188 EXPORT_SYMBOL(hpsb_lock);
1190 EXPORT_SYMBOL(hpsb_register_highlevel);
1191 EXPORT_SYMBOL(hpsb_unregister_highlevel);
1192 EXPORT_SYMBOL(hpsb_register_addrspace);
1193 EXPORT_SYMBOL(hpsb_unregister_addrspace);
1194 EXPORT_SYMBOL(hpsb_listen_channel);
1195 EXPORT_SYMBOL(hpsb_unlisten_channel);
1196 EXPORT_SYMBOL(highlevel_read);
1197 EXPORT_SYMBOL(highlevel_write);
1198 EXPORT_SYMBOL(highlevel_lock);
1199 EXPORT_SYMBOL(highlevel_lock64);
1200 EXPORT_SYMBOL(highlevel_add_host);
1201 EXPORT_SYMBOL(highlevel_remove_host);
1202 EXPORT_SYMBOL(highlevel_host_reset);
1204 EXPORT_SYMBOL(hpsb_guid_get_entry);
1205 EXPORT_SYMBOL(hpsb_nodeid_get_entry);
1206 EXPORT_SYMBOL(hpsb_node_fill_packet);
1207 EXPORT_SYMBOL(hpsb_node_read);
1208 EXPORT_SYMBOL(hpsb_node_write);
1209 EXPORT_SYMBOL(hpsb_node_lock);
1210 EXPORT_SYMBOL(hpsb_update_config_rom);
1211 EXPORT_SYMBOL(hpsb_get_config_rom);
1212 EXPORT_SYMBOL(hpsb_register_protocol);
1213 EXPORT_SYMBOL(hpsb_unregister_protocol);
1214 EXPORT_SYMBOL(hpsb_release_unit_directory);
1216 EXPORT_SYMBOL(ieee1394_register_chardev);
1217 EXPORT_SYMBOL(ieee1394_unregister_chardev);
1218 EXPORT_SYMBOL(ieee1394_devfs_handle);
1220 EXPORT_SYMBOL(ieee1394_procfs_entry);