1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Intel USBIO Bridge driver
4 *
5 * Copyright (c) 2025 Intel Corporation.
6 * Copyright (c) 2025 Red Hat, Inc.
7 */
8
9 #include <linux/acpi.h>
10 #include <linux/auxiliary_bus.h>
11 #include <linux/byteorder/generic.h>
12 #include <linux/cleanup.h>
13 #include <linux/completion.h>
14 #include <linux/dev_printk.h>
15 #include <linux/device.h>
16 #include <linux/lockdep.h>
17 #include <linux/mutex.h>
18 #include <linux/string.h>
19 #include <linux/types.h>
20 #include <linux/usb.h>
21 #include <linux/usb/usbio.h>
22
23 /*************************************
24 * USBIO Bridge Protocol Definitions *
25 *************************************/
26
27 /* USBIO Control Commands */
28 #define USBIO_CTRLCMD_PROTVER 0
29 #define USBIO_CTRLCMD_FWVER 1
30 #define USBIO_CTRLCMD_HS 2
31 #define USBIO_CTRLCMD_ENUMGPIO 16
32 #define USBIO_CTRLCMD_ENUMI2C 17
33
34 /* USBIO Packet Flags */
35 #define USBIO_PKTFLAG_ACK BIT(0)
36 #define USBIO_PKTFLAG_RSP BIT(1)
37 #define USBIO_PKTFLAG_CMP BIT(2)
38 #define USBIO_PKTFLAG_ERR BIT(3)
39
40 #define USBIO_PKTFLAGS_REQRESP (USBIO_PKTFLAG_CMP | USBIO_PKTFLAG_ACK)
41
42 #define USBIO_CTRLXFER_TIMEOUT 0
43 #define USBIO_BULKXFER_TIMEOUT 100
44
45 struct usbio_protver {
46 u8 ver;
47 } __packed;
48
49 struct usbio_fwver {
50 u8 major;
51 u8 minor;
52 __le16 patch;
53 __le16 build;
54 } __packed;
55
56 /***********************************
57 * USBIO Bridge Device Definitions *
58 ***********************************/
59
60 /**
61 * struct usbio_device - the usb device exposing IOs
62 *
63 * @dev: the device in the usb interface
64 * @udev: the detected usb device
65 * @intf: the usb interface
66 * @quirks: quirks
67 * @ctrl_mutex: protects ctrl_buf
68 * @ctrl_pipe: the control transfer pipe
69 * @ctrlbuf_len: the size of the control transfer pipe
70 * @ctrlbuf: the buffer used for control transfers
71 * @bulk_mutex: protects tx_buf, rx_buf and split bulk-transfers getting interrupted
72 * @tx_pipe: the bulk out pipe
73 * @txbuf_len: the size of the bulk out pipe
74 * @txbuf: the buffer used for bulk out transfers
75 * @rx_pipe: the bulk in pipe
76 * @rxbuf_len: the size of the bulk in pipe
77 * @rxdat_len: the data length at rx buffer
78 * @rxbuf: the buffer used for bulk in transfers
79 * @urb: the urb to read bulk pipe
80 * @done: completion object as request is done
81 * @cli_list: device's client list
82 * @nr_gpio_banks: Number of GPIO banks
83 * @gpios: GPIO bank descriptors
84 * @nr_gpio_banks: Number of I2C busses
85 * @gpios: I2C bank descriptors
86 */
87 struct usbio_device {
88 struct device *dev;
89 struct usb_device *udev;
90 struct usb_interface *intf;
91 unsigned long quirks;
92
93 struct mutex ctrl_mutex;
94 unsigned int ctrl_pipe;
95 u16 ctrlbuf_len;
96 void *ctrlbuf;
97
98 struct mutex bulk_mutex;
99 unsigned int tx_pipe;
100 u16 txbuf_len;
101 void *txbuf;
102
103 unsigned int rx_pipe;
104 u16 rxbuf_len;
105 u16 rxdat_len;
106 void *rxbuf;
107 struct urb *urb;
108
109 struct completion done;
110
111 struct list_head cli_list;
112
113 unsigned int nr_gpio_banks;
114 struct usbio_gpio_bank_desc gpios[USBIO_MAX_GPIOBANKS];
115
116 unsigned int nr_i2c_buses;
117 struct usbio_i2c_bus_desc i2cs[USBIO_MAX_I2CBUSES];
118 };
119
120 /**
121 * struct usbio_client - represents a usbio client
122 *
123 * @auxdev: auxiliary device object
124 * @mutex: protects @bridge
125 * @bridge: usbio bridge who service the client
126 * @link: usbio bridge clients list member
127 */
128 struct usbio_client {
129 struct auxiliary_device auxdev;
130 struct mutex mutex;
131 struct usbio_device *bridge;
132 struct list_head link;
133 };
134
135 #define adev_to_client(adev) container_of_const(adev, struct usbio_client, auxdev)
136
usbio_ctrl_msg(struct usbio_device * usbio,u8 type,u8 cmd,const void * obuf,u16 obuf_len,void * ibuf,u16 ibuf_len)137 static int usbio_ctrl_msg(struct usbio_device *usbio, u8 type, u8 cmd,
138 const void *obuf, u16 obuf_len, void *ibuf, u16 ibuf_len)
139 {
140 u8 request = USB_TYPE_VENDOR | USB_RECIP_DEVICE;
141 struct usbio_ctrl_packet *cpkt;
142 unsigned int pipe;
143 u16 cpkt_len;
144 int ret;
145
146 lockdep_assert_held(&usbio->ctrl_mutex);
147
148 if ((obuf_len > (usbio->ctrlbuf_len - sizeof(*cpkt))) ||
149 (ibuf_len > (usbio->ctrlbuf_len - sizeof(*cpkt))))
150 return -EMSGSIZE;
151
152 /* Prepare Control Packet Header */
153 cpkt = usbio->ctrlbuf;
154 cpkt->header.type = type;
155 cpkt->header.cmd = cmd;
156 if (type == USBIO_PKTTYPE_CTRL || ibuf_len)
157 cpkt->header.flags = USBIO_PKTFLAGS_REQRESP;
158 else
159 cpkt->header.flags = USBIO_PKTFLAG_CMP;
160 cpkt->len = obuf_len;
161
162 /* Copy the data */
163 memcpy(cpkt->data, obuf, obuf_len);
164
165 pipe = usb_sndctrlpipe(usbio->udev, usbio->ctrl_pipe);
166 cpkt_len = sizeof(*cpkt) + obuf_len;
167 ret = usb_control_msg(usbio->udev, pipe, 0, request | USB_DIR_OUT, 0, 0,
168 cpkt, cpkt_len, USBIO_CTRLXFER_TIMEOUT);
169 dev_dbg(usbio->dev, "control out %d hdr %*phN data %*phN\n", ret,
170 (int)sizeof(*cpkt), cpkt, (int)cpkt->len, cpkt->data);
171
172 if (ret != cpkt_len) {
173 dev_err(usbio->dev, "USB control out failed: %d\n", ret);
174 return (ret < 0) ? ret : -EPROTO;
175 }
176
177 if (!(cpkt->header.flags & USBIO_PKTFLAG_ACK))
178 return 0;
179
180 pipe = usb_rcvctrlpipe(usbio->udev, usbio->ctrl_pipe);
181 cpkt_len = sizeof(*cpkt) + ibuf_len;
182 ret = usb_control_msg(usbio->udev, pipe, 0, request | USB_DIR_IN, 0, 0,
183 cpkt, cpkt_len, USBIO_CTRLXFER_TIMEOUT);
184 dev_dbg(usbio->dev, "control in %d hdr %*phN data %*phN\n", ret,
185 (int)sizeof(*cpkt), cpkt, (int)cpkt->len, cpkt->data);
186
187 if (ret < sizeof(*cpkt)) {
188 dev_err(usbio->dev, "USB control in failed: %d\n", ret);
189 return (ret < 0) ? ret : -EPROTO;
190 }
191
192 if (cpkt->header.type != type || cpkt->header.cmd != cmd ||
193 !(cpkt->header.flags & USBIO_PKTFLAG_RSP)) {
194 dev_err(usbio->dev, "Unexpected reply type: %u, cmd: %u, flags: %u\n",
195 cpkt->header.type, cpkt->header.cmd, cpkt->header.flags);
196 return -EPROTO;
197 }
198
199 if (cpkt->header.flags & USBIO_PKTFLAG_ERR)
200 return -EREMOTEIO;
201
202 if (ibuf_len < cpkt->len)
203 return -ENOSPC;
204
205 memcpy(ibuf, cpkt->data, cpkt->len);
206
207 return cpkt->len;
208 }
209
usbio_control_msg(struct auxiliary_device * adev,u8 type,u8 cmd,const void * obuf,u16 obuf_len,void * ibuf,u16 ibuf_len)210 int usbio_control_msg(struct auxiliary_device *adev, u8 type, u8 cmd,
211 const void *obuf, u16 obuf_len, void *ibuf, u16 ibuf_len)
212 {
213 struct usbio_client *client = adev_to_client(adev);
214 struct usbio_device *usbio;
215 int ret;
216
217 guard(mutex)(&client->mutex);
218
219 usbio = client->bridge;
220 if (!usbio)
221 return -ENODEV; /* Disconnected */
222
223 ret = usb_autopm_get_interface(usbio->intf);
224 if (ret)
225 return ret;
226
227 mutex_lock(&usbio->ctrl_mutex);
228
229 ret = usbio_ctrl_msg(client->bridge, type, cmd, obuf, obuf_len, ibuf, ibuf_len);
230
231 mutex_unlock(&usbio->ctrl_mutex);
232 usb_autopm_put_interface(usbio->intf);
233
234 return ret;
235 }
236 EXPORT_SYMBOL_NS_GPL(usbio_control_msg, "USBIO");
237
usbio_bulk_recv(struct urb * urb)238 static void usbio_bulk_recv(struct urb *urb)
239 {
240 struct usbio_bulk_packet *bpkt = urb->transfer_buffer;
241 struct usbio_device *usbio = urb->context;
242
243 if (!urb->status) {
244 if (bpkt->header.flags & USBIO_PKTFLAG_RSP) {
245 usbio->rxdat_len = urb->actual_length;
246 complete(&usbio->done);
247 }
248 } else if (urb->status != -ENOENT) {
249 dev_err(usbio->dev, "Bulk in error %d\n", urb->status);
250 }
251
252 usb_submit_urb(usbio->urb, GFP_ATOMIC);
253 }
254
usbio_bulk_msg(struct auxiliary_device * adev,u8 type,u8 cmd,bool last,const void * obuf,u16 obuf_len,void * ibuf,u16 ibuf_len)255 int usbio_bulk_msg(struct auxiliary_device *adev, u8 type, u8 cmd, bool last,
256 const void *obuf, u16 obuf_len, void *ibuf, u16 ibuf_len)
257 {
258 struct usbio_client *client = adev_to_client(adev);
259 struct usbio_device *usbio = client->bridge;
260 struct usbio_bulk_packet *bpkt;
261 int ret, act = 0;
262 u16 bpkt_len;
263
264 lockdep_assert_held(&client->mutex);
265 lockdep_assert_held(&usbio->bulk_mutex);
266
267 if ((obuf_len > (usbio->txbuf_len - sizeof(*bpkt))) ||
268 (ibuf_len > (usbio->rxbuf_len - sizeof(*bpkt))))
269 return -EMSGSIZE;
270
271 if (ibuf_len)
272 reinit_completion(&usbio->done);
273
274 /* If no data to send, skip to read */
275 if (!obuf_len)
276 goto read;
277
278 /* Prepare Bulk Packet Header */
279 bpkt = usbio->txbuf;
280 bpkt->header.type = type;
281 bpkt->header.cmd = cmd;
282 if (!last)
283 bpkt->header.flags = 0;
284 else if (ibuf_len)
285 bpkt->header.flags = USBIO_PKTFLAGS_REQRESP;
286 else
287 bpkt->header.flags = USBIO_PKTFLAG_CMP;
288 bpkt->len = cpu_to_le16(obuf_len);
289
290 /* Copy the data */
291 memcpy(bpkt->data, obuf, obuf_len);
292
293 bpkt_len = sizeof(*bpkt) + obuf_len;
294 ret = usb_bulk_msg(usbio->udev, usbio->tx_pipe, bpkt, bpkt_len, &act,
295 USBIO_BULKXFER_TIMEOUT);
296 dev_dbg(usbio->dev, "bulk out %d hdr %*phN data %*phN\n", act,
297 (int)sizeof(*bpkt), bpkt, obuf_len, bpkt->data);
298
299 if (ret || act != bpkt_len) {
300 dev_err(usbio->dev, "Bulk out failed: %d\n", ret);
301 return ret ?: -EPROTO;
302 }
303
304 if (!(bpkt->header.flags & USBIO_PKTFLAG_ACK))
305 return obuf_len;
306
307 read:
308 ret = wait_for_completion_timeout(&usbio->done, USBIO_BULKXFER_TIMEOUT);
309 if (ret <= 0) {
310 dev_err(usbio->dev, "Bulk in wait failed: %d\n", ret);
311 return ret ?: -ETIMEDOUT;
312 }
313
314 act = usbio->rxdat_len;
315 bpkt = usbio->rxbuf;
316 bpkt_len = le16_to_cpu(bpkt->len);
317 dev_dbg(usbio->dev, "bulk in %d hdr %*phN data %*phN\n", act,
318 (int)sizeof(*bpkt), bpkt, bpkt_len, bpkt->data);
319
320 /*
321 * Unsupported bulk commands get only an usbio_packet_header with
322 * the error flag set as reply. Return -EPIPE for this case.
323 */
324 if (act == sizeof(struct usbio_packet_header) &&
325 (bpkt->header.flags & USBIO_PKTFLAG_ERR))
326 return -EPIPE;
327
328 if (act < sizeof(*bpkt)) {
329 dev_err(usbio->dev, "Bulk in short read: %d\n", act);
330 return -EPROTO;
331 }
332
333 if (bpkt->header.type != type || bpkt->header.cmd != cmd ||
334 !(bpkt->header.flags & USBIO_PKTFLAG_RSP)) {
335 dev_err(usbio->dev,
336 "Unexpected bulk in type 0x%02x cmd 0x%02x flags 0x%02x\n",
337 bpkt->header.type, bpkt->header.cmd, bpkt->header.flags);
338 return -EPROTO;
339 }
340
341 if (bpkt->header.flags & USBIO_PKTFLAG_ERR)
342 return -EREMOTEIO;
343
344 if (ibuf_len < bpkt_len)
345 return -ENOSPC;
346
347 /* The device must not claim more payload than it actually sent. */
348 if (bpkt_len > act - sizeof(*bpkt))
349 return -EPROTO;
350
351 memcpy(ibuf, bpkt->data, bpkt_len);
352
353 return bpkt_len;
354 }
355 EXPORT_SYMBOL_NS_GPL(usbio_bulk_msg, "USBIO");
356
usbio_acquire(struct auxiliary_device * adev)357 int usbio_acquire(struct auxiliary_device *adev)
358 {
359 struct usbio_client *client = adev_to_client(adev);
360 struct usbio_device *usbio;
361 int ret;
362
363 mutex_lock(&client->mutex);
364
365 usbio = client->bridge;
366 if (!usbio) {
367 ret = -ENODEV; /* Disconnected */
368 goto err_unlock;
369 }
370
371 ret = usb_autopm_get_interface(usbio->intf);
372 if (ret)
373 goto err_unlock;
374
375 mutex_lock(&usbio->bulk_mutex);
376
377 /* Leave client locked until release to avoid abba deadlock issues */
378 return 0;
379
380 err_unlock:
381 mutex_unlock(&client->mutex);
382
383 return ret;
384 }
385 EXPORT_SYMBOL_NS_GPL(usbio_acquire, "USBIO");
386
usbio_release(struct auxiliary_device * adev)387 void usbio_release(struct auxiliary_device *adev)
388 {
389 struct usbio_client *client = adev_to_client(adev);
390 struct usbio_device *usbio = client->bridge;
391
392 lockdep_assert_held(&client->mutex);
393
394 mutex_unlock(&usbio->bulk_mutex);
395 usb_autopm_put_interface(usbio->intf);
396 mutex_unlock(&client->mutex);
397 }
398 EXPORT_SYMBOL_NS_GPL(usbio_release, "USBIO");
399
usbio_get_txrxbuf_len(struct auxiliary_device * adev,u16 * txbuf_len,u16 * rxbuf_len)400 void usbio_get_txrxbuf_len(struct auxiliary_device *adev, u16 *txbuf_len, u16 *rxbuf_len)
401 {
402 struct usbio_client *client = adev_to_client(adev);
403 struct usbio_device *usbio;
404
405 guard(mutex)(&client->mutex);
406
407 usbio = client->bridge;
408 if (!usbio)
409 return; /* Disconnected */
410
411 *txbuf_len = usbio->txbuf_len;
412 *rxbuf_len = usbio->rxbuf_len;
413 }
414 EXPORT_SYMBOL_NS_GPL(usbio_get_txrxbuf_len, "USBIO");
415
usbio_get_quirks(struct auxiliary_device * adev)416 unsigned long usbio_get_quirks(struct auxiliary_device *adev)
417 {
418 struct usbio_client *client = adev_to_client(adev);
419 struct usbio_device *usbio;
420
421 guard(mutex)(&client->mutex);
422
423 usbio = client->bridge;
424 if (!usbio)
425 return 0; /* Disconnected */
426
427 return usbio->quirks;
428 }
429 EXPORT_SYMBOL_NS_GPL(usbio_get_quirks, "USBIO");
430
usbio_auxdev_release(struct device * dev)431 static void usbio_auxdev_release(struct device *dev)
432 {
433 struct auxiliary_device *adev = to_auxiliary_dev(dev);
434 struct usbio_client *client = adev_to_client(adev);
435
436 mutex_destroy(&client->mutex);
437 kfree(client);
438 }
439
usbio_add_client(struct usbio_device * usbio,char * name,u8 id,void * data)440 static int usbio_add_client(struct usbio_device *usbio, char *name, u8 id, void *data)
441 {
442 struct usbio_client *client;
443 struct auxiliary_device *adev;
444 int ret;
445
446 client = kzalloc_obj(*client);
447 if (!client)
448 return -ENOMEM;
449
450 mutex_init(&client->mutex);
451 client->bridge = usbio;
452 adev = &client->auxdev;
453 adev->name = name;
454 adev->id = id;
455
456 adev->dev.parent = usbio->dev;
457 adev->dev.platform_data = data;
458 adev->dev.release = usbio_auxdev_release;
459
460 ret = auxiliary_device_init(adev);
461 if (ret) {
462 usbio_auxdev_release(&adev->dev);
463 return ret;
464 }
465
466 ret = auxiliary_device_add(adev);
467 if (ret) {
468 auxiliary_device_uninit(adev);
469 return ret;
470 }
471
472 list_add_tail(&client->link, &usbio->cli_list);
473
474 return 0;
475 }
476
usbio_enum_gpios(struct usbio_device * usbio)477 static int usbio_enum_gpios(struct usbio_device *usbio)
478 {
479 struct usbio_gpio_bank_desc *gpio = usbio->gpios;
480
481 dev_dbg(usbio->dev, "GPIO Banks: %d\n", usbio->nr_gpio_banks);
482
483 for (unsigned int i = 0; i < usbio->nr_gpio_banks; i++)
484 dev_dbg(usbio->dev, "\tBank%d[%d] map: %#08x\n",
485 gpio[i].id, gpio[i].pins, gpio[i].bmap);
486
487 usbio_add_client(usbio, USBIO_GPIO_CLIENT, 0, gpio);
488
489 return 0;
490 }
491
usbio_enum_i2cs(struct usbio_device * usbio)492 static int usbio_enum_i2cs(struct usbio_device *usbio)
493 {
494 struct usbio_i2c_bus_desc *i2c = usbio->i2cs;
495
496 dev_dbg(usbio->dev, "I2C Busses: %d\n", usbio->nr_i2c_buses);
497
498 for (unsigned int i = 0; i < usbio->nr_i2c_buses; i++) {
499 dev_dbg(usbio->dev, "\tBus%d caps: %#02x\n", i2c[i].id, i2c[i].caps);
500 usbio_add_client(usbio, USBIO_I2C_CLIENT, i, &i2c[i]);
501 }
502
503 return 0;
504 }
505
usbio_suspend(struct usb_interface * intf,pm_message_t msg)506 static int usbio_suspend(struct usb_interface *intf, pm_message_t msg)
507 {
508 struct usbio_device *usbio = usb_get_intfdata(intf);
509
510 usb_kill_urb(usbio->urb);
511
512 return 0;
513 }
514
usbio_resume(struct usb_interface * intf)515 static int usbio_resume(struct usb_interface *intf)
516 {
517 struct usbio_device *usbio = usb_get_intfdata(intf);
518
519 return usb_submit_urb(usbio->urb, GFP_KERNEL);
520 }
521
usbio_disconnect(struct usb_interface * intf)522 static void usbio_disconnect(struct usb_interface *intf)
523 {
524 struct usbio_device *usbio = usb_get_intfdata(intf);
525 struct usbio_client *client, *next;
526
527 /* Wakeup any clients waiting for a reply */
528 usbio->rxdat_len = 0;
529 complete(&usbio->done);
530
531 /* Let clients know the bridge is gone */
532 list_for_each_entry(client, &usbio->cli_list, link) {
533 mutex_lock(&client->mutex);
534 client->bridge = NULL;
535 mutex_unlock(&client->mutex);
536 }
537
538 /* From here on clients will no longer touch struct usbio_device */
539 usb_kill_urb(usbio->urb);
540 usb_free_urb(usbio->urb);
541
542 list_for_each_entry_safe_reverse(client, next, &usbio->cli_list, link) {
543 auxiliary_device_delete(&client->auxdev);
544 auxiliary_device_uninit(&client->auxdev);
545 }
546 }
547
usbio_probe(struct usb_interface * intf,const struct usb_device_id * id)548 static int usbio_probe(struct usb_interface *intf, const struct usb_device_id *id)
549 {
550 struct usb_device *udev = interface_to_usbdev(intf);
551 struct usb_endpoint_descriptor *ep_in, *ep_out;
552 struct device *dev = &intf->dev;
553 struct usbio_protver protver;
554 struct usbio_device *usbio;
555 struct usbio_fwver fwver;
556 int ret;
557
558 usbio = devm_kzalloc(dev, sizeof(*usbio), GFP_KERNEL);
559 if (!usbio)
560 return -ENOMEM;
561
562 ret = devm_mutex_init(dev, &usbio->ctrl_mutex);
563 if (ret)
564 return ret;
565
566 ret = devm_mutex_init(dev, &usbio->bulk_mutex);
567 if (ret)
568 return ret;
569
570 usbio->dev = dev;
571 usbio->udev = udev;
572 usbio->intf = intf;
573 usbio->quirks = id ? id->driver_info : 0;
574 init_completion(&usbio->done);
575 INIT_LIST_HEAD(&usbio->cli_list);
576 usb_set_intfdata(intf, usbio);
577
578 usbio->ctrl_pipe = usb_endpoint_num(&udev->ep0.desc);
579 usbio->ctrlbuf_len = usb_maxpacket(udev, usbio->ctrl_pipe);
580 usbio->ctrlbuf = devm_kzalloc(dev, usbio->ctrlbuf_len, GFP_KERNEL);
581 if (!usbio->ctrlbuf)
582 return -ENOMEM;
583
584 /* Find the first bulk-in and bulk-out endpoints */
585 ret = usb_find_common_endpoints(intf->cur_altsetting, &ep_in, &ep_out,
586 NULL, NULL);
587 if (ret) {
588 dev_err(dev, "Cannot find bulk endpoints: %d\n", ret);
589 return ret;
590 }
591
592 usbio->tx_pipe = usb_sndbulkpipe(udev, usb_endpoint_num(ep_out));
593
594 if (usbio->quirks & USBIO_QUIRK_BULK_MAXP_63)
595 usbio->txbuf_len = 63;
596 else
597 usbio->txbuf_len = usb_endpoint_maxp(ep_out);
598
599 usbio->txbuf = devm_kzalloc(dev, usbio->txbuf_len, GFP_KERNEL);
600 if (!usbio->txbuf)
601 return -ENOMEM;
602
603 usbio->rx_pipe = usb_rcvbulkpipe(udev, usb_endpoint_num(ep_in));
604
605 if (usbio->quirks & USBIO_QUIRK_BULK_MAXP_63)
606 usbio->rxbuf_len = 63;
607 else
608 usbio->rxbuf_len = usb_endpoint_maxp(ep_in);
609
610 usbio->rxbuf = devm_kzalloc(dev, usbio->rxbuf_len, GFP_KERNEL);
611 if (!usbio->rxbuf)
612 return -ENOMEM;
613
614 usbio->urb = usb_alloc_urb(0, GFP_KERNEL);
615 if (!usbio->urb)
616 return -ENOMEM;
617
618 usb_fill_bulk_urb(usbio->urb, udev, usbio->rx_pipe, usbio->rxbuf,
619 usbio->rxbuf_len, usbio_bulk_recv, usbio);
620 ret = usb_submit_urb(usbio->urb, GFP_KERNEL);
621 if (ret) {
622 dev_err_probe(dev, ret, "Submitting usb urb\n");
623 goto err_free_urb;
624 }
625
626 mutex_lock(&usbio->ctrl_mutex);
627
628 ret = usbio_ctrl_msg(usbio, USBIO_PKTTYPE_CTRL, USBIO_CTRLCMD_HS, NULL, 0, NULL, 0);
629 if (ret < 0)
630 goto err_unlock;
631
632 ret = usbio_ctrl_msg(usbio, USBIO_PKTTYPE_CTRL, USBIO_CTRLCMD_PROTVER, NULL, 0,
633 &protver, sizeof(protver));
634 if (ret < 0)
635 goto err_unlock;
636
637 ret = usbio_ctrl_msg(usbio, USBIO_PKTTYPE_CTRL, USBIO_CTRLCMD_FWVER, NULL, 0,
638 &fwver, sizeof(fwver));
639 if (ret < 0)
640 goto err_unlock;
641
642 ret = usbio_ctrl_msg(usbio, USBIO_PKTTYPE_CTRL, USBIO_CTRLCMD_ENUMGPIO, NULL, 0,
643 usbio->gpios, sizeof(usbio->gpios));
644 if (ret < 0 || ret % sizeof(struct usbio_gpio_bank_desc)) {
645 ret = (ret < 0) ? ret : -EPROTO;
646 goto err_unlock;
647 }
648 usbio->nr_gpio_banks = ret / sizeof(struct usbio_gpio_bank_desc);
649
650 ret = usbio_ctrl_msg(usbio, USBIO_PKTTYPE_CTRL, USBIO_CTRLCMD_ENUMI2C, NULL, 0,
651 usbio->i2cs, sizeof(usbio->i2cs));
652 if (ret < 0 || ret % sizeof(struct usbio_i2c_bus_desc)) {
653 ret = (ret < 0) ? ret : -EPROTO;
654 goto err_unlock;
655 }
656 usbio->nr_i2c_buses = ret / sizeof(struct usbio_i2c_bus_desc);
657
658 mutex_unlock(&usbio->ctrl_mutex);
659
660 dev_dbg(dev, "ProtVer(BCD): %02x FwVer: %d.%d.%d.%d\n",
661 protver.ver, fwver.major, fwver.minor,
662 le16_to_cpu(fwver.patch), le16_to_cpu(fwver.build));
663
664 usbio_enum_gpios(usbio);
665 usbio_enum_i2cs(usbio);
666
667 return 0;
668
669 err_unlock:
670 mutex_unlock(&usbio->ctrl_mutex);
671 usb_kill_urb(usbio->urb);
672 err_free_urb:
673 usb_free_urb(usbio->urb);
674
675 return ret;
676 }
677
678 static const struct usb_device_id usbio_table[] = {
679 { USB_DEVICE(0x2ac1, 0x20c1), /* Lattice NX40 */
680 .driver_info = USBIO_QUIRK_I2C_MAX_RW_LEN_52 },
681 { USB_DEVICE(0x2ac1, 0x20c9), /* Lattice NX33 */
682 .driver_info = USBIO_QUIRK_I2C_NO_INIT_ACK | USBIO_QUIRK_I2C_MAX_RW_LEN_52 |
683 USBIO_QUIRK_I2C_ALLOW_400KHZ },
684 { USB_DEVICE(0x2ac1, 0x20cb) }, /* Lattice NX33U */
685 { USB_DEVICE(0x06cb, 0x0701), /* Synaptics Sabre */
686 .driver_info = USBIO_QUIRK_BULK_MAXP_63 | USBIO_QUIRK_I2C_USE_CHUNK_LEN },
687 { }
688 };
689 MODULE_DEVICE_TABLE(usb, usbio_table);
690
691 static struct usb_driver usbio_driver = {
692 .name = "usbio-bridge",
693 .probe = usbio_probe,
694 .disconnect = usbio_disconnect,
695 .suspend = usbio_suspend,
696 .resume = usbio_resume,
697 .id_table = usbio_table,
698 .supports_autosuspend = 1,
699 };
700 module_usb_driver(usbio_driver);
701
702 struct usbio_match_ids_walk_data {
703 struct acpi_device *adev;
704 const struct acpi_device_id *hids;
705 unsigned int id;
706 };
707
usbio_match_device_ids(struct acpi_device * adev,void * data)708 static int usbio_match_device_ids(struct acpi_device *adev, void *data)
709 {
710 struct usbio_match_ids_walk_data *wd = data;
711 unsigned int id = 0;
712 char *uid;
713
714 if (acpi_match_device_ids(adev, wd->hids))
715 return 0;
716
717 uid = acpi_device_uid(adev);
718 if (uid) {
719 for (int i = 0; i < strlen(uid); i++) {
720 if (!kstrtouint(&uid[i], 10, &id))
721 break;
722 }
723 }
724
725 if (!uid || wd->id == id) {
726 wd->adev = adev;
727 return 1;
728 }
729
730 return 0;
731 }
732
usbio_acpi_bind(struct auxiliary_device * adev,const struct acpi_device_id * hids)733 void usbio_acpi_bind(struct auxiliary_device *adev, const struct acpi_device_id *hids)
734 {
735 struct device *dev = &adev->dev;
736 struct acpi_device *parent;
737 struct usbio_match_ids_walk_data wd = {
738 .adev = NULL,
739 .hids = hids,
740 .id = adev->id,
741 };
742
743 parent = ACPI_COMPANION(dev->parent);
744 if (!parent)
745 return;
746
747 acpi_dev_for_each_child(parent, usbio_match_device_ids, &wd);
748 if (wd.adev)
749 ACPI_COMPANION_SET(dev, wd.adev);
750 }
751 EXPORT_SYMBOL_NS_GPL(usbio_acpi_bind, "USBIO");
752
753 MODULE_DESCRIPTION("Intel USBIO Bridge driver");
754 MODULE_AUTHOR("Israel Cepeda <israel.a.cepeda.lopez@intel.com>");
755 MODULE_AUTHOR("Hans de Goede <hansg@kernel.org>");
756 MODULE_LICENSE("GPL");
757