blob: 1b9f60a22e0b4167171d38a288316c4600e64b04 [file] [log] [blame]
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001/*
2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
Nobuo Iwata0775a9c2016-06-13 11:33:40 +09003 * Copyright (C) 2015-2016 Nobuo Iwata
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06004 *
5 * This is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18 * USA.
19 */
20
matt mooney7aaacb42011-05-11 22:33:43 -070021#include <linux/kthread.h>
Bernard Blackham3d0a2a22012-10-22 06:45:00 +110022#include <linux/file.h>
matt mooney7aaacb42011-05-11 22:33:43 -070023#include <linux/net.h>
Nobuo Iwata0775a9c2016-06-13 11:33:40 +090024#include <linux/platform_device.h>
25#include <linux/slab.h>
matt mooney7aaacb42011-05-11 22:33:43 -070026
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060027#include "usbip_common.h"
28#include "vhci.h"
29
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060030/* TODO: refine locking ?*/
31
Yuyang Du1c9de5b2017-06-08 13:04:10 +080032/*
33 * output example:
34 * hub port sta spd dev socket local_busid
35 * hs 0000 004 000 00000000 c5a7bb80 1-2.3
36 * ................................................
37 * ss 0008 004 000 00000000 d8cee980 2-3.4
38 * ................................................
39 *
40 * IP address can be retrieved from a socket pointer address by looking
41 * up /proc/net/{tcp,tcp6}. Also, a userland program may remember a
42 * port number and its peer IP address.
43 */
44static void port_show_vhci(char **out, int hub, int port, struct vhci_device *vdev)
45{
46 if (hub == HUB_SPEED_HIGH)
47 *out += sprintf(*out, "hs %04u %03u ",
48 port, vdev->ud.status);
49 else /* hub == HUB_SPEED_SUPER */
50 *out += sprintf(*out, "ss %04u %03u ",
51 port, vdev->ud.status);
52
53 if (vdev->ud.status == VDEV_ST_USED) {
54 *out += sprintf(*out, "%03u %08x ",
55 vdev->speed, vdev->devid);
56 *out += sprintf(*out, "%16p %s",
57 vdev->ud.tcp_socket,
58 dev_name(&vdev->udev->dev));
59
60 } else {
61 *out += sprintf(*out, "000 00000000 ");
62 *out += sprintf(*out, "0000000000000000 0-0");
63 }
64
65 *out += sprintf(*out, "\n");
66}
67
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060068/* Sysfs entry to show port status */
Nobuo Iwata0775a9c2016-06-13 11:33:40 +090069static ssize_t status_show_vhci(int pdev_nr, char *out)
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060070{
Yuyang Du89a73d22017-06-08 13:04:07 +080071 struct platform_device *pdev = vhcis[pdev_nr].pdev;
Yuyang Du03cd00d2017-06-08 13:04:09 +080072 struct vhci *vhci;
73 struct usb_hcd *hcd;
74 struct vhci_hcd *vhci_hcd;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060075 char *s = out;
Yuyang Du03cd00d2017-06-08 13:04:09 +080076 int i;
Andrew Goodbody21619792016-02-02 17:36:39 +000077 unsigned long flags;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060078
Nobuo Iwata0775a9c2016-06-13 11:33:40 +090079 if (!pdev || !out) {
80 usbip_dbg_vhci_sysfs("show status error\n");
81 return 0;
82 }
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060083
Yuyang Du03cd00d2017-06-08 13:04:09 +080084 hcd = platform_get_drvdata(pdev);
85 vhci_hcd = hcd_to_vhci_hcd(hcd);
86 vhci = vhci_hcd->vhci;
Nobuo Iwata0775a9c2016-06-13 11:33:40 +090087
88 spin_lock_irqsave(&vhci->lock, flags);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060089
Nobuo Iwata0775a9c2016-06-13 11:33:40 +090090 for (i = 0; i < VHCI_HC_PORTS; i++) {
Yuyang Du1c9de5b2017-06-08 13:04:10 +080091 struct vhci_device *vdev = &vhci->vhci_hcd_hs->vdev[i];
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060092
93 spin_lock(&vdev->ud.lock);
Yuyang Du1c9de5b2017-06-08 13:04:10 +080094 port_show_vhci(&out, HUB_SPEED_HIGH,
Yuyang Dub8912452017-06-08 13:04:13 +080095 pdev_nr * VHCI_PORTS + i, vdev);
Yuyang Du1c9de5b2017-06-08 13:04:10 +080096 spin_unlock(&vdev->ud.lock);
97 }
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060098
Yuyang Du1c9de5b2017-06-08 13:04:10 +080099 for (i = 0; i < VHCI_HC_PORTS; i++) {
100 struct vhci_device *vdev = &vhci->vhci_hcd_ss->vdev[i];
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600101
Yuyang Du1c9de5b2017-06-08 13:04:10 +0800102 spin_lock(&vdev->ud.lock);
103 port_show_vhci(&out, HUB_SPEED_SUPER,
Yuyang Dub8912452017-06-08 13:04:13 +0800104 pdev_nr * VHCI_PORTS + VHCI_HC_PORTS + i, vdev);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600105 spin_unlock(&vdev->ud.lock);
106 }
107
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900108 spin_unlock_irqrestore(&vhci->lock, flags);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600109
110 return out - s;
111}
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900112
113static ssize_t status_show_not_ready(int pdev_nr, char *out)
114{
115 char *s = out;
116 int i = 0;
117
118 for (i = 0; i < VHCI_HC_PORTS; i++) {
Yuyang Du1c9de5b2017-06-08 13:04:10 +0800119 out += sprintf(out, "hs %04u %03u ",
Yuyang Dub8912452017-06-08 13:04:13 +0800120 (pdev_nr * VHCI_PORTS) + i,
Yuyang Du1c9de5b2017-06-08 13:04:10 +0800121 VDEV_ST_NOTASSIGNED);
122 out += sprintf(out, "000 00000000 0000000000000000 0-0");
123 out += sprintf(out, "\n");
124 }
125
126 for (i = 0; i < VHCI_HC_PORTS; i++) {
127 out += sprintf(out, "ss %04u %03u ",
Yuyang Dub8912452017-06-08 13:04:13 +0800128 (pdev_nr * VHCI_PORTS) + VHCI_HC_PORTS + i,
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900129 VDEV_ST_NOTASSIGNED);
130 out += sprintf(out, "000 00000000 0000000000000000 0-0");
131 out += sprintf(out, "\n");
132 }
133 return out - s;
134}
135
136static int status_name_to_id(const char *name)
137{
138 char *c;
139 long val;
140 int ret;
141
142 c = strchr(name, '.');
143 if (c == NULL)
144 return 0;
145
146 ret = kstrtol(c+1, 10, &val);
147 if (ret < 0)
148 return ret;
149
150 return val;
151}
152
153static ssize_t status_show(struct device *dev,
154 struct device_attribute *attr, char *out)
155{
156 char *s = out;
157 int pdev_nr;
158
159 out += sprintf(out,
Yuyang Du1c9de5b2017-06-08 13:04:10 +0800160 "hub port sta spd dev socket local_busid\n");
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900161
162 pdev_nr = status_name_to_id(attr->attr.name);
163 if (pdev_nr < 0)
164 out += status_show_not_ready(pdev_nr, out);
165 else
166 out += status_show_vhci(pdev_nr, out);
167
168 return out - s;
169}
170
171static ssize_t nports_show(struct device *dev, struct device_attribute *attr,
172 char *out)
173{
174 char *s = out;
175
Yuyang Du1c9de5b2017-06-08 13:04:10 +0800176 /*
177 * Half the ports are for SPEED_HIGH and half for SPEED_SUPER, thus the * 2.
178 */
Yuyang Dub8912452017-06-08 13:04:13 +0800179 out += sprintf(out, "%d\n", VHCI_PORTS * vhci_num_controllers);
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900180 return out - s;
181}
182static DEVICE_ATTR_RO(nports);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600183
184/* Sysfs entry to shutdown a virtual connection */
Yuyang Du03cd00d2017-06-08 13:04:09 +0800185static int vhci_port_disconnect(struct vhci_hcd *vhci_hcd, __u32 rhport)
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600186{
Yuyang Du03cd00d2017-06-08 13:04:09 +0800187 struct vhci_device *vdev = &vhci_hcd->vdev[rhport];
188 struct vhci *vhci = vhci_hcd->vhci;
Andrew Goodbody21619792016-02-02 17:36:39 +0000189 unsigned long flags;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600190
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600191 usbip_dbg_vhci_sysfs("enter\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600192
193 /* lock */
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900194 spin_lock_irqsave(&vhci->lock, flags);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600195 spin_lock(&vdev->ud.lock);
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900196
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600197 if (vdev->ud.status == VDEV_ST_NULL) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700198 pr_err("not connected %d\n", vdev->ud.status);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600199
200 /* unlock */
201 spin_unlock(&vdev->ud.lock);
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900202 spin_unlock_irqrestore(&vhci->lock, flags);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600203
204 return -EINVAL;
205 }
206
207 /* unlock */
208 spin_unlock(&vdev->ud.lock);
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900209 spin_unlock_irqrestore(&vhci->lock, flags);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600210
211 usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN);
212
213 return 0;
214}
215
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900216static int valid_port(__u32 pdev_nr, __u32 rhport)
217{
218 if (pdev_nr >= vhci_num_controllers) {
219 pr_err("pdev %u\n", pdev_nr);
220 return 0;
221 }
222 if (rhport >= VHCI_HC_PORTS) {
223 pr_err("rhport %u\n", rhport);
224 return 0;
225 }
226 return 1;
227}
228
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600229static ssize_t store_detach(struct device *dev, struct device_attribute *attr,
230 const char *buf, size_t count)
231{
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900232 __u32 port = 0, pdev_nr = 0, rhport = 0;
233 struct usb_hcd *hcd;
Yuyang Du1c9de5b2017-06-08 13:04:10 +0800234 struct vhci_hcd *vhci_hcd;
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900235 int ret;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600236
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900237 if (kstrtoint(buf, 10, &port) < 0)
John de la Garza88fa1eb2014-03-06 10:36:34 -0800238 return -EINVAL;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600239
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900240 pdev_nr = port_to_pdev_nr(port);
241 rhport = port_to_rhport(port);
242
243 if (!valid_port(pdev_nr, rhport))
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600244 return -EINVAL;
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900245
Yuyang Du89a73d22017-06-08 13:04:07 +0800246 hcd = platform_get_drvdata(vhcis[pdev_nr].pdev);
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900247 if (hcd == NULL) {
248 dev_err(dev, "port is not ready %u\n", port);
249 return -EAGAIN;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600250 }
251
Yuyang Du1c9de5b2017-06-08 13:04:10 +0800252 usbip_dbg_vhci_sysfs("rhport %d\n", rhport);
253
254 if ((port / VHCI_HC_PORTS) % 2)
255 vhci_hcd = hcd_to_vhci_hcd(hcd)->vhci->vhci_hcd_ss;
256 else
257 vhci_hcd = hcd_to_vhci_hcd(hcd)->vhci->vhci_hcd_hs;
258
259 ret = vhci_port_disconnect(vhci_hcd, rhport);
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900260 if (ret < 0)
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600261 return -EINVAL;
262
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600263 usbip_dbg_vhci_sysfs("Leave\n");
matt mooneybd608f62011-05-06 03:47:52 -0700264
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600265 return count;
266}
267static DEVICE_ATTR(detach, S_IWUSR, NULL, store_detach);
268
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900269static int valid_args(__u32 pdev_nr, __u32 rhport, enum usb_device_speed speed)
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600270{
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900271 if (!valid_port(pdev_nr, rhport)) {
272 return 0;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600273 }
274
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600275 switch (speed) {
276 case USB_SPEED_LOW:
277 case USB_SPEED_FULL:
278 case USB_SPEED_HIGH:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -0800279 case USB_SPEED_WIRELESS:
Yuyang Dudf9032c2017-06-08 13:04:11 +0800280 case USB_SPEED_SUPER:
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600281 break;
282 default:
Shuah Khan8360fb02014-01-22 09:38:14 -0700283 pr_err("Failed attach request for unsupported USB speed: %s\n",
284 usb_speed_string(speed));
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900285 return 0;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600286 }
287
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900288 return 1;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600289}
290
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900291/* Sysfs entry to establish a virtual connection */
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600292/*
293 * To start a new USB/IP attachment, a userland program needs to setup a TCP
294 * connection and then write its socket descriptor with remote device
295 * information into this sysfs file.
296 *
297 * A remote device is virtually attached to the root-hub port of @rhport with
298 * @speed. @devid is embedded into a request to specify the remote device in a
299 * server host.
300 *
301 * write() returns 0 on success, else negative errno.
302 */
303static ssize_t store_attach(struct device *dev, struct device_attribute *attr,
304 const char *buf, size_t count)
305{
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600306 struct socket *socket;
307 int sockfd = 0;
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900308 __u32 port = 0, pdev_nr = 0, rhport = 0, devid = 0, speed = 0;
309 struct usb_hcd *hcd;
Yuyang Du03cd00d2017-06-08 13:04:09 +0800310 struct vhci_hcd *vhci_hcd;
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900311 struct vhci_device *vdev;
Yuyang Du03cd00d2017-06-08 13:04:09 +0800312 struct vhci *vhci;
Al Viro964ea962014-03-05 20:33:08 -0500313 int err;
Andrew Goodbody21619792016-02-02 17:36:39 +0000314 unsigned long flags;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600315
316 /*
317 * @rhport: port number of vhci_hcd
318 * @sockfd: socket descriptor of an established TCP connection
319 * @devid: unique device identifier in a remote host
320 * @speed: usb device speed in a remote host
321 */
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900322 if (sscanf(buf, "%u %u %u %u", &port, &sockfd, &devid, &speed) != 4)
John de la Garza88fa1eb2014-03-06 10:36:34 -0800323 return -EINVAL;
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900324 pdev_nr = port_to_pdev_nr(port);
325 rhport = port_to_rhport(port);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600326
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900327 usbip_dbg_vhci_sysfs("port(%u) pdev(%d) rhport(%u)\n",
328 port, pdev_nr, rhport);
329 usbip_dbg_vhci_sysfs("sockfd(%u) devid(%u) speed(%u)\n",
330 sockfd, devid, speed);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600331
332 /* check received parameters */
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900333 if (!valid_args(pdev_nr, rhport, speed))
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600334 return -EINVAL;
335
Yuyang Du89a73d22017-06-08 13:04:07 +0800336 hcd = platform_get_drvdata(vhcis[pdev_nr].pdev);
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900337 if (hcd == NULL) {
338 dev_err(dev, "port %d is not ready\n", port);
339 return -EAGAIN;
340 }
Yuyang Du03cd00d2017-06-08 13:04:09 +0800341
342 vhci_hcd = hcd_to_vhci_hcd(hcd);
343 vhci = vhci_hcd->vhci;
Yuyang Du1c9de5b2017-06-08 13:04:10 +0800344
345 if (speed == USB_SPEED_SUPER)
346 vdev = &vhci->vhci_hcd_ss->vdev[rhport];
347 else
348 vdev = &vhci->vhci_hcd_hs->vdev[rhport];
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900349
Bernard Blackham3d0a2a22012-10-22 06:45:00 +1100350 /* Extract socket from fd. */
Al Viro964ea962014-03-05 20:33:08 -0500351 socket = sockfd_lookup(sockfd, &err);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600352 if (!socket)
Márton Németha6d81814a2011-05-27 06:18:48 +0200353 return -EINVAL;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600354
355 /* now need lock until setting vdev status as used */
356
357 /* begin a lock */
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900358 spin_lock_irqsave(&vhci->lock, flags);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600359 spin_lock(&vdev->ud.lock);
360
361 if (vdev->ud.status != VDEV_ST_NULL) {
362 /* end of the lock */
363 spin_unlock(&vdev->ud.lock);
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900364 spin_unlock_irqrestore(&vhci->lock, flags);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600365
Al Viro964ea962014-03-05 20:33:08 -0500366 sockfd_put(socket);
Bernard Blackham3d0a2a22012-10-22 06:45:00 +1100367
matt mooney1a4b6f62011-05-19 16:47:32 -0700368 dev_err(dev, "port %d already used\n", rhport);
Nobuo Iwataa38711a2017-08-29 11:13:34 +0900369 /*
370 * Will be retried from userspace
371 * if there's another free port.
372 */
373 return -EBUSY;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600374 }
375
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900376 dev_info(dev, "pdev(%u) rhport(%u) sockfd(%d)\n",
377 pdev_nr, rhport, sockfd);
378 dev_info(dev, "devid(%u) speed(%u) speed_str(%s)\n",
379 devid, speed, usb_speed_string(speed));
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600380
381 vdev->devid = devid;
382 vdev->speed = speed;
383 vdev->ud.tcp_socket = socket;
384 vdev->ud.status = VDEV_ST_NOTASSIGNED;
385
386 spin_unlock(&vdev->ud.lock);
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900387 spin_unlock_irqrestore(&vhci->lock, flags);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600388 /* end the lock */
389
Oleg Nesterovba46ce32012-03-13 19:07:18 +0100390 vdev->ud.tcp_rx = kthread_get_run(vhci_rx_loop, &vdev->ud, "vhci_rx");
391 vdev->ud.tcp_tx = kthread_get_run(vhci_tx_loop, &vdev->ud, "vhci_tx");
Max Vozelerd1b2e952011-04-18 21:44:10 +0200392
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900393 rh_port_connect(vdev, speed);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600394
395 return count;
396}
397static DEVICE_ATTR(attach, S_IWUSR, NULL, store_attach);
398
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900399#define MAX_STATUS_NAME 16
400
401struct status_attr {
402 struct device_attribute attr;
403 char name[MAX_STATUS_NAME+1];
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600404};
405
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900406static struct status_attr *status_attrs;
407
408static void set_status_attr(int id)
409{
410 struct status_attr *status;
411
412 status = status_attrs + id;
413 if (id == 0)
414 strcpy(status->name, "status");
415 else
416 snprintf(status->name, MAX_STATUS_NAME+1, "status.%d", id);
417 status->attr.attr.name = status->name;
418 status->attr.attr.mode = S_IRUGO;
419 status->attr.show = status_show;
Shuah Khan918b8ac2016-12-05 12:56:38 -0700420 sysfs_attr_init(&status->attr.attr);
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900421}
422
423static int init_status_attrs(void)
424{
425 int id;
426
427 status_attrs = kcalloc(vhci_num_controllers, sizeof(struct status_attr),
428 GFP_KERNEL);
429 if (status_attrs == NULL)
430 return -ENOMEM;
431
432 for (id = 0; id < vhci_num_controllers; id++)
433 set_status_attr(id);
434
435 return 0;
436}
437
438static void finish_status_attrs(void)
439{
440 kfree(status_attrs);
441}
442
443struct attribute_group vhci_attr_group = {
444 .attrs = NULL,
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600445};
Nobuo Iwata0775a9c2016-06-13 11:33:40 +0900446
447int vhci_init_attr_group(void)
448{
449 struct attribute **attrs;
450 int ret, i;
451
452 attrs = kcalloc((vhci_num_controllers + 5), sizeof(struct attribute *),
453 GFP_KERNEL);
454 if (attrs == NULL)
455 return -ENOMEM;
456
457 ret = init_status_attrs();
458 if (ret) {
459 kfree(attrs);
460 return ret;
461 }
462 *attrs = &dev_attr_nports.attr;
463 *(attrs + 1) = &dev_attr_detach.attr;
464 *(attrs + 2) = &dev_attr_attach.attr;
465 *(attrs + 3) = &dev_attr_usbip_debug.attr;
466 for (i = 0; i < vhci_num_controllers; i++)
467 *(attrs + i + 4) = &((status_attrs + i)->attr.attr);
468 vhci_attr_group.attrs = attrs;
469 return 0;
470}
471
472void vhci_finish_attr_group(void)
473{
474 finish_status_attrs();
475 kfree(vhci_attr_group.attrs);
476}