Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/agpgart
[linux-2.6] / drivers / isdn / sc / packet.c
1 /* $Id: packet.c,v 1.5.8.1 2001/09/23 22:24:59 kai Exp $
2  *
3  * Copyright (C) 1996  SpellCaster Telecommunications Inc.
4  *
5  * This software may be used and distributed according to the terms
6  * of the GNU General Public License, incorporated herein by reference.
7  *
8  * For more information, please contact gpl-info@spellcast.com or write:
9  *
10  *     SpellCaster Telecommunications Inc.
11  *     5621 Finch Avenue East, Unit #3
12  *     Scarborough, Ontario  Canada
13  *     M1B 2T9
14  *     +1 (416) 297-8565
15  *     +1 (416) 297-6433 Facsimile
16  */
17
18 #include "includes.h"
19 #include "hardware.h"
20 #include "message.h"
21 #include "card.h"
22
23 int sndpkt(int devId, int channel, struct sk_buff *data)
24 {
25         LLData  ReqLnkWrite;
26         int status;
27         int card;
28         unsigned long len;
29
30         card = get_card_from_id(devId);
31
32         if(!IS_VALID_CARD(card)) {
33                 pr_debug("invalid param: %d is not a valid card id\n", card);
34                 return -ENODEV;
35         }
36
37         pr_debug("%s: sndpkt: frst = 0x%lx nxt = %d  f = %d n = %d\n",
38                 sc_adapter[card]->devicename,
39                 sc_adapter[card]->channel[channel].first_sendbuf,
40                 sc_adapter[card]->channel[channel].next_sendbuf,
41                 sc_adapter[card]->channel[channel].free_sendbufs,
42                 sc_adapter[card]->channel[channel].num_sendbufs);
43
44         if(!sc_adapter[card]->channel[channel].free_sendbufs) {
45                 pr_debug("%s: out of TX buffers\n",
46                                 sc_adapter[card]->devicename);
47                 return -EINVAL;
48         }
49
50         if(data->len > BUFFER_SIZE) {
51                 pr_debug("%s: data overflows buffer size (data > buffer)\n",
52                         sc_adapter[card]->devicename);
53                 return -EINVAL;
54         }
55
56         ReqLnkWrite.buff_offset = sc_adapter[card]->channel[channel].next_sendbuf *
57                 BUFFER_SIZE + sc_adapter[card]->channel[channel].first_sendbuf;
58         ReqLnkWrite.msg_len = data->len; /* sk_buff size */
59         pr_debug("%s: writing %d bytes to buffer offset 0x%lx\n",
60                         sc_adapter[card]->devicename,
61                         ReqLnkWrite.msg_len, ReqLnkWrite.buff_offset);
62         memcpy_toshmem(card, (char *)ReqLnkWrite.buff_offset, data->data, ReqLnkWrite.msg_len);
63
64         /*
65          * sendmessage
66          */
67         pr_debug("%s: sndpkt size=%d, buf_offset=0x%lx buf_indx=%d\n",
68                 sc_adapter[card]->devicename,
69                 ReqLnkWrite.msg_len, ReqLnkWrite.buff_offset,
70                 sc_adapter[card]->channel[channel].next_sendbuf);
71
72         status = sendmessage(card, CEPID, ceReqTypeLnk, ceReqClass1, ceReqLnkWrite,
73                                 channel+1, sizeof(LLData), (unsigned int*)&ReqLnkWrite);
74         len = data->len;
75         if(status) {
76                 pr_debug("%s: failed to send packet, status = %d\n",
77                                 sc_adapter[card]->devicename, status);
78                 return -1;
79         }
80         else {
81                 sc_adapter[card]->channel[channel].free_sendbufs--;
82                 sc_adapter[card]->channel[channel].next_sendbuf =
83                         ++sc_adapter[card]->channel[channel].next_sendbuf ==
84                         sc_adapter[card]->channel[channel].num_sendbufs ? 0 :
85                         sc_adapter[card]->channel[channel].next_sendbuf;
86                         pr_debug("%s: packet sent successfully\n", sc_adapter[card]->devicename);
87                 dev_kfree_skb(data);
88                 indicate_status(card,ISDN_STAT_BSENT,channel, (char *)&len);
89         }
90         return len;
91 }
92
93 void rcvpkt(int card, RspMessage *rcvmsg)
94 {
95         LLData newll;
96         struct sk_buff *skb;
97
98         if(!IS_VALID_CARD(card)) {
99                 pr_debug("invalid param: %d is not a valid card id\n", card);
100                 return;
101         }
102
103         switch(rcvmsg->rsp_status){
104         case 0x01:
105         case 0x02:
106         case 0x70:
107                 pr_debug("%s: error status code: 0x%x\n",
108                         sc_adapter[card]->devicename, rcvmsg->rsp_status);
109                 return;
110         case 0x00: 
111             if (!(skb = dev_alloc_skb(rcvmsg->msg_data.response.msg_len))) {
112                         printk(KERN_WARNING "%s: rcvpkt out of memory, dropping packet\n",
113                                 sc_adapter[card]->devicename);
114                         return;
115                 }
116                 skb_put(skb, rcvmsg->msg_data.response.msg_len);
117                 pr_debug("%s: getting data from offset: 0x%lx\n",
118                         sc_adapter[card]->devicename,
119                         rcvmsg->msg_data.response.buff_offset);
120                 memcpy_fromshmem(card,
121                         skb_put(skb, rcvmsg->msg_data.response.msg_len),
122                         (char *)rcvmsg->msg_data.response.buff_offset,
123                         rcvmsg->msg_data.response.msg_len);
124                 sc_adapter[card]->card->rcvcallb_skb(sc_adapter[card]->driverId,
125                         rcvmsg->phy_link_no-1, skb);
126
127         case 0x03:
128                 /*
129                  * Recycle the buffer
130                  */
131                 pr_debug("%s: buffer size : %d\n",
132                                 sc_adapter[card]->devicename, BUFFER_SIZE);
133 /*              memset_shmem(card, rcvmsg->msg_data.response.buff_offset, 0, BUFFER_SIZE); */
134                 newll.buff_offset = rcvmsg->msg_data.response.buff_offset;
135                 newll.msg_len = BUFFER_SIZE;
136                 pr_debug("%s: recycled buffer at offset 0x%lx size %d\n",
137                         sc_adapter[card]->devicename,
138                         newll.buff_offset, newll.msg_len);
139                 sendmessage(card, CEPID, ceReqTypeLnk, ceReqClass1, ceReqLnkRead,
140                         rcvmsg->phy_link_no, sizeof(LLData), (unsigned int *)&newll);
141         }
142
143 }
144
145 int setup_buffers(int card, int c)
146 {
147         unsigned int nBuffers, i, cBase;
148         unsigned int buffer_size;
149         LLData  RcvBuffOffset;
150
151         if(!IS_VALID_CARD(card)) {
152                 pr_debug("invalid param: %d is not a valid card id\n", card);
153                 return -ENODEV;
154         }
155
156         /*
157          * Calculate the buffer offsets (send/recv/send/recv)
158          */
159         pr_debug("%s: setting up channel buffer space in shared RAM\n",
160                         sc_adapter[card]->devicename);
161         buffer_size = BUFFER_SIZE;
162         nBuffers = ((sc_adapter[card]->ramsize - BUFFER_BASE) / buffer_size) / 2;
163         nBuffers = nBuffers > BUFFERS_MAX ? BUFFERS_MAX : nBuffers;
164         pr_debug("%s: calculating buffer space: %d buffers, %d big\n",
165                 sc_adapter[card]->devicename,
166                 nBuffers, buffer_size);
167         if(nBuffers < 2) {
168                 pr_debug("%s: not enough buffer space\n",
169                         sc_adapter[card]->devicename);
170                 return -1;
171         }
172         cBase = (nBuffers * buffer_size) * (c - 1);
173         pr_debug("%s: channel buffer offset from shared RAM: 0x%x\n",
174                         sc_adapter[card]->devicename, cBase);
175         sc_adapter[card]->channel[c-1].first_sendbuf = BUFFER_BASE + cBase;
176         sc_adapter[card]->channel[c-1].num_sendbufs = nBuffers / 2;
177         sc_adapter[card]->channel[c-1].free_sendbufs = nBuffers / 2;
178         sc_adapter[card]->channel[c-1].next_sendbuf = 0;
179         pr_debug("%s: send buffer setup complete: first=0x%lx n=%d f=%d, nxt=%d\n",
180                                 sc_adapter[card]->devicename,
181                                 sc_adapter[card]->channel[c-1].first_sendbuf,
182                                 sc_adapter[card]->channel[c-1].num_sendbufs,
183                                 sc_adapter[card]->channel[c-1].free_sendbufs,
184                                 sc_adapter[card]->channel[c-1].next_sendbuf);
185
186         /*
187          * Prep the receive buffers
188          */
189         pr_debug("%s: adding %d RecvBuffers:\n",
190                         sc_adapter[card]->devicename, nBuffers /2);
191         for (i = 0 ; i < nBuffers / 2; i++) {
192                 RcvBuffOffset.buff_offset = 
193                         ((sc_adapter[card]->channel[c-1].first_sendbuf +
194                         (nBuffers / 2) * buffer_size) + (buffer_size * i));
195                 RcvBuffOffset.msg_len = buffer_size;
196                 pr_debug("%s: adding RcvBuffer #%d offset=0x%lx sz=%d bufsz:%d\n",
197                                 sc_adapter[card]->devicename,
198                                 i + 1, RcvBuffOffset.buff_offset, 
199                                 RcvBuffOffset.msg_len,buffer_size);
200                 sendmessage(card, CEPID, ceReqTypeLnk, ceReqClass1, ceReqLnkRead,
201                                 c, sizeof(LLData), (unsigned int *)&RcvBuffOffset);
202         } 
203         return 0;
204 }
205