qlge: Remove spinlock from asic init path.
[linux-2.6] / drivers / staging / usbip / vhci_tx.c
1 /*
2  * Copyright (C) 2003-2008 Takahiro Hirofuchi
3  *
4  * This is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17  * USA.
18  */
19
20 #include "usbip_common.h"
21 #include "vhci.h"
22
23
24 static void setup_cmd_submit_pdu(struct usbip_header *pdup,  struct urb *urb)
25 {
26         struct vhci_priv *priv = ((struct vhci_priv *)urb->hcpriv);
27         struct vhci_device *vdev = priv->vdev;
28
29         dbg_vhci_tx("URB, local devnum %u, remote devid %u\n",
30                                 usb_pipedevice(urb->pipe), vdev->devid);
31
32         pdup->base.command = USBIP_CMD_SUBMIT;
33         pdup->base.seqnum  = priv->seqnum;
34         pdup->base.devid   = vdev->devid;
35         if (usb_pipein(urb->pipe))
36                 pdup->base.direction = USBIP_DIR_IN;
37         else
38                 pdup->base.direction = USBIP_DIR_OUT;
39         pdup->base.ep      = usb_pipeendpoint(urb->pipe);
40
41         usbip_pack_pdu(pdup, urb, USBIP_CMD_SUBMIT, 1);
42
43         if (urb->setup_packet)
44                 memcpy(pdup->u.cmd_submit.setup, urb->setup_packet, 8);
45 }
46
47 static struct vhci_priv *dequeue_from_priv_tx(struct vhci_device *vdev)
48 {
49         unsigned long flags;
50         struct vhci_priv *priv, *tmp;
51
52         spin_lock_irqsave(&vdev->priv_lock, flags);
53
54         list_for_each_entry_safe(priv, tmp, &vdev->priv_tx, list) {
55                 list_move_tail(&priv->list, &vdev->priv_rx);
56                 spin_unlock_irqrestore(&vdev->priv_lock, flags);
57                 return priv;
58         }
59
60         spin_unlock_irqrestore(&vdev->priv_lock, flags);
61
62         return NULL;
63 }
64
65
66
67 static int vhci_send_cmd_submit(struct vhci_device *vdev)
68 {
69         struct vhci_priv *priv = NULL;
70
71         struct msghdr msg;
72         struct kvec iov[3];
73         size_t txsize;
74
75         size_t total_size = 0;
76
77         while ((priv = dequeue_from_priv_tx(vdev)) != NULL) {
78                 int ret;
79                 struct urb *urb = priv->urb;
80                 struct usbip_header pdu_header;
81                 void *iso_buffer = NULL;
82
83                 txsize = 0;
84                 memset(&pdu_header, 0, sizeof(pdu_header));
85                 memset(&msg, 0, sizeof(msg));
86                 memset(&iov, 0, sizeof(iov));
87
88                 dbg_vhci_tx("setup txdata urb %p\n", urb);
89
90
91                 /* 1. setup usbip_header */
92                 setup_cmd_submit_pdu(&pdu_header, urb);
93                 usbip_header_correct_endian(&pdu_header, 1);
94
95                 iov[0].iov_base = &pdu_header;
96                 iov[0].iov_len  = sizeof(pdu_header);
97                 txsize += sizeof(pdu_header);
98
99                 /* 2. setup transfer buffer */
100                 if (!usb_pipein(urb->pipe) && urb->transfer_buffer_length > 0) {
101                         iov[1].iov_base = urb->transfer_buffer;
102                         iov[1].iov_len  = urb->transfer_buffer_length;
103                         txsize += urb->transfer_buffer_length;
104                 }
105
106                 /* 3. setup iso_packet_descriptor */
107                 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
108                         ssize_t len = 0;
109
110                         iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
111                         if (!iso_buffer) {
112                                 usbip_event_add(&vdev->ud,
113                                                 SDEV_EVENT_ERROR_MALLOC);
114                                 return -1;
115                         }
116
117                         iov[2].iov_base = iso_buffer;
118                         iov[2].iov_len  = len;
119                         txsize += len;
120                 }
121
122                 ret = kernel_sendmsg(vdev->ud.tcp_socket, &msg, iov, 3, txsize);
123                 if (ret != txsize) {
124                         uerr("sendmsg failed!, retval %d for %zd\n", ret,
125                                                                 txsize);
126                         kfree(iso_buffer);
127                         usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_TCP);
128                         return -1;
129                 }
130
131                 kfree(iso_buffer);
132                 dbg_vhci_tx("send txdata\n");
133
134                 total_size += txsize;
135         }
136
137         return total_size;
138 }
139
140
141 /*-------------------------------------------------------------------------*/
142
143 static struct vhci_unlink *dequeue_from_unlink_tx(struct vhci_device *vdev)
144 {
145         unsigned long flags;
146         struct vhci_unlink *unlink, *tmp;
147
148         spin_lock_irqsave(&vdev->priv_lock, flags);
149
150         list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
151                 list_move_tail(&unlink->list, &vdev->unlink_rx);
152                 spin_unlock_irqrestore(&vdev->priv_lock, flags);
153                 return unlink;
154         }
155
156         spin_unlock_irqrestore(&vdev->priv_lock, flags);
157
158         return NULL;
159 }
160
161 static int vhci_send_cmd_unlink(struct vhci_device *vdev)
162 {
163         struct vhci_unlink *unlink = NULL;
164
165         struct msghdr msg;
166         struct kvec iov[3];
167         size_t txsize;
168
169         size_t total_size = 0;
170
171         while ((unlink = dequeue_from_unlink_tx(vdev)) != NULL) {
172                 int ret;
173                 struct usbip_header pdu_header;
174
175                 txsize = 0;
176                 memset(&pdu_header, 0, sizeof(pdu_header));
177                 memset(&msg, 0, sizeof(msg));
178                 memset(&iov, 0, sizeof(iov));
179
180                 dbg_vhci_tx("setup cmd unlink, %lu \n", unlink->seqnum);
181
182
183                 /* 1. setup usbip_header */
184                 pdu_header.base.command = USBIP_CMD_UNLINK;
185                 pdu_header.base.seqnum  = unlink->seqnum;
186                 pdu_header.base.devid   = vdev->devid;
187                 pdu_header.base.ep      = 0;
188                 pdu_header.u.cmd_unlink.seqnum = unlink->unlink_seqnum;
189
190                 usbip_header_correct_endian(&pdu_header, 1);
191
192                 iov[0].iov_base = &pdu_header;
193                 iov[0].iov_len  = sizeof(pdu_header);
194                 txsize += sizeof(pdu_header);
195
196                 ret = kernel_sendmsg(vdev->ud.tcp_socket, &msg, iov, 1, txsize);
197                 if (ret != txsize) {
198                         uerr("sendmsg failed!, retval %d for %zd\n", ret,
199                                                                 txsize);
200                         usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_TCP);
201                         return -1;
202                 }
203
204
205                 dbg_vhci_tx("send txdata\n");
206
207                 total_size += txsize;
208         }
209
210         return total_size;
211 }
212
213
214 /*-------------------------------------------------------------------------*/
215
216 void vhci_tx_loop(struct usbip_task *ut)
217 {
218         struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_tx);
219         struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
220
221         while (1) {
222                 if (signal_pending(current)) {
223                         uinfo("vhci_tx signal catched\n");
224                         break;
225                 }
226
227                 if (vhci_send_cmd_submit(vdev) < 0)
228                         break;
229
230                 if (vhci_send_cmd_unlink(vdev) < 0)
231                         break;
232
233                 wait_event_interruptible(vdev->waitq_tx,
234                                 (!list_empty(&vdev->priv_tx) ||
235                                  !list_empty(&vdev->unlink_tx)));
236
237                 dbg_vhci_tx("pending urbs ?, now wake up\n");
238         }
239 }