1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Intel La Jolla Cove Adapter USB driver
4 *
5 * Copyright (c) 2023, Intel Corporation.
6 */
7
8 #include <linux/acpi.h>
9 #include <linux/auxiliary_bus.h>
10 #include <linux/dev_printk.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16 #include <linux/types.h>
17 #include <linux/usb.h>
18 #include <linux/usb/ljca.h>
19
20 #include <linux/unaligned.h>
21
22 /* command flags */
23 #define LJCA_ACK_FLAG BIT(0)
24 #define LJCA_RESP_FLAG BIT(1)
25 #define LJCA_CMPL_FLAG BIT(2)
26
27 #define LJCA_MAX_PACKET_SIZE 64u
28 #define LJCA_MAX_PAYLOAD_SIZE \
29 (LJCA_MAX_PACKET_SIZE - sizeof(struct ljca_msg))
30
31 #define LJCA_WRITE_TIMEOUT_MS 200
32 #define LJCA_WRITE_ACK_TIMEOUT_MS 500
33 #define LJCA_ENUM_CLIENT_TIMEOUT_MS 20
34
35 /* ljca client type */
36 enum ljca_client_type {
37 LJCA_CLIENT_MNG = 1,
38 LJCA_CLIENT_GPIO = 3,
39 LJCA_CLIENT_I2C = 4,
40 LJCA_CLIENT_SPI = 5,
41 };
42
43 /* MNG client commands */
44 enum ljca_mng_cmd {
45 LJCA_MNG_RESET = 2,
46 LJCA_MNG_ENUM_GPIO = 4,
47 LJCA_MNG_ENUM_I2C = 5,
48 LJCA_MNG_ENUM_SPI = 8,
49 };
50
51 /* ljca client acpi _ADR */
52 enum ljca_client_acpi_adr {
53 LJCA_GPIO_ACPI_ADR,
54 LJCA_I2C1_ACPI_ADR,
55 LJCA_I2C2_ACPI_ADR,
56 LJCA_SPI1_ACPI_ADR,
57 LJCA_SPI2_ACPI_ADR,
58 LJCA_CLIENT_ACPI_ADR_MAX,
59 };
60
61 /* ljca cmd message structure */
62 struct ljca_msg {
63 u8 type;
64 u8 cmd;
65 u8 flags;
66 u8 len;
67 u8 data[] __counted_by(len);
68 } __packed;
69
70 struct ljca_i2c_ctr_info {
71 u8 id;
72 u8 capacity;
73 u8 intr_pin;
74 } __packed;
75
76 struct ljca_i2c_descriptor {
77 u8 num;
78 struct ljca_i2c_ctr_info info[] __counted_by(num);
79 } __packed;
80
81 struct ljca_spi_ctr_info {
82 u8 id;
83 u8 capacity;
84 u8 intr_pin;
85 } __packed;
86
87 struct ljca_spi_descriptor {
88 u8 num;
89 struct ljca_spi_ctr_info info[] __counted_by(num);
90 } __packed;
91
92 struct ljca_bank_descriptor {
93 u8 bank_id;
94 u8 pin_num;
95
96 /* 1 bit for each gpio, 1 means valid */
97 __le32 valid_pins;
98 } __packed;
99
100 struct ljca_gpio_descriptor {
101 u8 pins_per_bank;
102 u8 bank_num;
103 struct ljca_bank_descriptor bank_desc[] __counted_by(bank_num);
104 } __packed;
105
106 /**
107 * struct ljca_adapter - represent a ljca adapter
108 *
109 * @intf: the usb interface for this ljca adapter
110 * @usb_dev: the usb device for this ljca adapter
111 * @dev: the specific device info of the usb interface
112 * @rx_pipe: bulk in pipe for receive data from firmware
113 * @tx_pipe: bulk out pipe for send data to firmware
114 * @rx_urb: urb used for the bulk in pipe
115 * @rx_buf: buffer used to receive command response and event
116 * @rx_len: length of rx buffer
117 * @ex_buf: external buffer to save command response
118 * @ex_buf_len: length of external buffer
119 * @actual_length: actual length of data copied to external buffer
120 * @tx_buf: buffer used to download command to firmware
121 * @tx_buf_len: length of tx buffer
122 * @lock: spinlock to protect tx_buf and ex_buf
123 * @cmd_completion: completion object as the command receives ack
124 * @mutex: mutex to avoid command download concurrently
125 * @client_list: client device list
126 * @disconnect: usb disconnect ongoing or not
127 * @reset_id: used to reset firmware
128 */
129 struct ljca_adapter {
130 struct usb_interface *intf;
131 struct usb_device *usb_dev;
132 struct device *dev;
133
134 unsigned int rx_pipe;
135 unsigned int tx_pipe;
136
137 struct urb *rx_urb;
138 void *rx_buf;
139 unsigned int rx_len;
140
141 u8 *ex_buf;
142 u8 ex_buf_len;
143 u8 actual_length;
144
145 void *tx_buf;
146 u8 tx_buf_len;
147
148 spinlock_t lock;
149
150 struct completion cmd_completion;
151 struct mutex mutex;
152
153 struct list_head client_list;
154
155 bool disconnect;
156
157 u32 reset_id;
158 };
159
160 struct ljca_match_ids_walk_data {
161 const struct acpi_device_id *ids;
162 const char *uid;
163 struct acpi_device *adev;
164 };
165
166 /*
167 * ACPI hardware IDs for LJCA client devices.
168 *
169 * [1] Some BIOS implementations use these IDs for denoting LJCA client devices
170 * even though the IDs have been allocated for USBIO. This isn't a problem
171 * as the usb-ljca driver is probed based on the USB device's vendor and
172 * product IDs and its client drivers are probed based on auxiliary device
173 * names, not these ACPI _HIDs. List of such systems:
174 *
175 * Dell Precision 5490
176 */
177 static const struct acpi_device_id ljca_gpio_hids[] = {
178 { "INTC100B" }, /* RPL LJCA GPIO */
179 { "INTC1074" }, /* CVF LJCA GPIO */
180 { "INTC1096" }, /* ADL LJCA GPIO */
181 { "INTC10B5" }, /* LNL LJCA GPIO */
182 { "INTC10D1" }, /* MTL (CVF VSC) USBIO GPIO [1] */
183 {},
184 };
185
186 static const struct acpi_device_id ljca_i2c_hids[] = {
187 { "INTC100C" }, /* RPL LJCA I2C */
188 { "INTC1075" }, /* CVF LJCA I2C */
189 { "INTC1097" }, /* ADL LJCA I2C */
190 { "INTC10D2" }, /* MTL (CVF VSC) USBIO I2C [1] */
191 {},
192 };
193
194 static const struct acpi_device_id ljca_spi_hids[] = {
195 { "INTC100D" }, /* RPL LJCA SPI */
196 { "INTC1091" }, /* TGL/ADL LJCA SPI */
197 { "INTC1098" }, /* ADL LJCA SPI */
198 { "INTC10D3" }, /* MTL (CVF VSC) USBIO SPI [1] */
199 {},
200 };
201
ljca_handle_event(struct ljca_adapter * adap,struct ljca_msg * header)202 static void ljca_handle_event(struct ljca_adapter *adap,
203 struct ljca_msg *header)
204 {
205 struct ljca_client *client;
206
207 list_for_each_entry(client, &adap->client_list, link) {
208 /*
209 * Currently only GPIO register event callback, but
210 * firmware message structure should include id when
211 * multiple same type clients register event callback.
212 */
213 if (client->type == header->type) {
214 unsigned long flags;
215
216 spin_lock_irqsave(&client->event_cb_lock, flags);
217 client->event_cb(client->context, header->cmd,
218 header->data, header->len);
219 spin_unlock_irqrestore(&client->event_cb_lock, flags);
220
221 break;
222 }
223 }
224 }
225
226 /* process command ack and received data if available */
ljca_handle_cmd_ack(struct ljca_adapter * adap,struct ljca_msg * header)227 static void ljca_handle_cmd_ack(struct ljca_adapter *adap, struct ljca_msg *header)
228 {
229 struct ljca_msg *tx_header = adap->tx_buf;
230 u8 ibuf_len, actual_len = 0;
231 unsigned long flags;
232 u8 *ibuf;
233
234 spin_lock_irqsave(&adap->lock, flags);
235
236 if (tx_header->type != header->type || tx_header->cmd != header->cmd) {
237 spin_unlock_irqrestore(&adap->lock, flags);
238 dev_err(adap->dev, "cmd ack mismatch error\n");
239 return;
240 }
241
242 ibuf_len = adap->ex_buf_len;
243 ibuf = adap->ex_buf;
244
245 if (ibuf && ibuf_len) {
246 actual_len = min(header->len, ibuf_len);
247
248 /* copy received data to external buffer */
249 memcpy(ibuf, header->data, actual_len);
250 }
251 /* update copied data length */
252 adap->actual_length = actual_len;
253
254 spin_unlock_irqrestore(&adap->lock, flags);
255
256 complete(&adap->cmd_completion);
257 }
258
ljca_recv(struct urb * urb)259 static void ljca_recv(struct urb *urb)
260 {
261 struct ljca_msg *header = urb->transfer_buffer;
262 struct ljca_adapter *adap = urb->context;
263 int ret;
264
265 switch (urb->status) {
266 case 0:
267 /* success */
268 break;
269 case -ENOENT:
270 /*
271 * directly complete the possible ongoing transfer
272 * during disconnect
273 */
274 if (adap->disconnect)
275 complete(&adap->cmd_completion);
276 return;
277 case -ECONNRESET:
278 case -ESHUTDOWN:
279 case -EPIPE:
280 /* rx urb is terminated */
281 dev_dbg(adap->dev, "rx urb terminated with status: %d\n",
282 urb->status);
283 return;
284 default:
285 dev_dbg(adap->dev, "rx urb error: %d\n", urb->status);
286 goto resubmit;
287 }
288
289 if (header->len + sizeof(*header) != urb->actual_length)
290 goto resubmit;
291
292 if (header->flags & LJCA_ACK_FLAG)
293 ljca_handle_cmd_ack(adap, header);
294 else
295 ljca_handle_event(adap, header);
296
297 resubmit:
298 ret = usb_submit_urb(urb, GFP_ATOMIC);
299 if (ret && ret != -EPERM)
300 dev_err(adap->dev, "resubmit rx urb error %d\n", ret);
301 }
302
ljca_send(struct ljca_adapter * adap,u8 type,u8 cmd,const u8 * obuf,u8 obuf_len,u8 * ibuf,u8 ibuf_len,bool ack,unsigned long timeout)303 static int ljca_send(struct ljca_adapter *adap, u8 type, u8 cmd,
304 const u8 *obuf, u8 obuf_len, u8 *ibuf, u8 ibuf_len,
305 bool ack, unsigned long timeout)
306 {
307 unsigned int msg_len = sizeof(struct ljca_msg) + obuf_len;
308 struct ljca_msg *header = adap->tx_buf;
309 unsigned int transferred;
310 unsigned long flags;
311 int ret;
312
313 if (adap->disconnect)
314 return -ENODEV;
315
316 if (msg_len > adap->tx_buf_len)
317 return -EINVAL;
318
319 mutex_lock(&adap->mutex);
320
321 spin_lock_irqsave(&adap->lock, flags);
322
323 header->type = type;
324 header->cmd = cmd;
325 header->len = obuf_len;
326 if (obuf)
327 memcpy(header->data, obuf, obuf_len);
328
329 header->flags = LJCA_CMPL_FLAG | (ack ? LJCA_ACK_FLAG : 0);
330
331 adap->ex_buf = ibuf;
332 adap->ex_buf_len = ibuf_len;
333 adap->actual_length = 0;
334
335 spin_unlock_irqrestore(&adap->lock, flags);
336
337 reinit_completion(&adap->cmd_completion);
338
339 ret = usb_autopm_get_interface(adap->intf);
340 if (ret < 0)
341 goto out;
342
343 ret = usb_bulk_msg(adap->usb_dev, adap->tx_pipe, header,
344 msg_len, &transferred, LJCA_WRITE_TIMEOUT_MS);
345 if (ret < 0)
346 goto out_put;
347 if (transferred != msg_len) {
348 ret = -EIO;
349 goto out_put;
350 }
351
352 if (ack) {
353 ret = wait_for_completion_timeout(&adap->cmd_completion,
354 timeout);
355 if (!ret) {
356 ret = -ETIMEDOUT;
357 goto out_put;
358 }
359 }
360 ret = adap->actual_length;
361
362 out_put:
363 usb_autopm_put_interface(adap->intf);
364
365 out:
366 spin_lock_irqsave(&adap->lock, flags);
367 adap->ex_buf = NULL;
368 adap->ex_buf_len = 0;
369
370 memset(header, 0, sizeof(*header));
371 spin_unlock_irqrestore(&adap->lock, flags);
372
373 mutex_unlock(&adap->mutex);
374
375 return ret;
376 }
377
ljca_transfer(struct ljca_client * client,u8 cmd,const u8 * obuf,u8 obuf_len,u8 * ibuf,u8 ibuf_len)378 int ljca_transfer(struct ljca_client *client, u8 cmd, const u8 *obuf,
379 u8 obuf_len, u8 *ibuf, u8 ibuf_len)
380 {
381 return ljca_send(client->adapter, client->type, cmd,
382 obuf, obuf_len, ibuf, ibuf_len, true,
383 LJCA_WRITE_ACK_TIMEOUT_MS);
384 }
385 EXPORT_SYMBOL_NS_GPL(ljca_transfer, "LJCA");
386
ljca_transfer_noack(struct ljca_client * client,u8 cmd,const u8 * obuf,u8 obuf_len)387 int ljca_transfer_noack(struct ljca_client *client, u8 cmd, const u8 *obuf,
388 u8 obuf_len)
389 {
390 return ljca_send(client->adapter, client->type, cmd, obuf,
391 obuf_len, NULL, 0, false, LJCA_WRITE_ACK_TIMEOUT_MS);
392 }
393 EXPORT_SYMBOL_NS_GPL(ljca_transfer_noack, "LJCA");
394
ljca_register_event_cb(struct ljca_client * client,ljca_event_cb_t event_cb,void * context)395 int ljca_register_event_cb(struct ljca_client *client, ljca_event_cb_t event_cb,
396 void *context)
397 {
398 unsigned long flags;
399
400 if (!event_cb)
401 return -EINVAL;
402
403 spin_lock_irqsave(&client->event_cb_lock, flags);
404
405 if (client->event_cb) {
406 spin_unlock_irqrestore(&client->event_cb_lock, flags);
407 return -EALREADY;
408 }
409
410 client->event_cb = event_cb;
411 client->context = context;
412
413 spin_unlock_irqrestore(&client->event_cb_lock, flags);
414
415 return 0;
416 }
417 EXPORT_SYMBOL_NS_GPL(ljca_register_event_cb, "LJCA");
418
ljca_unregister_event_cb(struct ljca_client * client)419 void ljca_unregister_event_cb(struct ljca_client *client)
420 {
421 unsigned long flags;
422
423 spin_lock_irqsave(&client->event_cb_lock, flags);
424
425 client->event_cb = NULL;
426 client->context = NULL;
427
428 spin_unlock_irqrestore(&client->event_cb_lock, flags);
429 }
430 EXPORT_SYMBOL_NS_GPL(ljca_unregister_event_cb, "LJCA");
431
ljca_match_device_ids(struct acpi_device * adev,void * data)432 static int ljca_match_device_ids(struct acpi_device *adev, void *data)
433 {
434 struct ljca_match_ids_walk_data *wd = data;
435 const char *uid = acpi_device_uid(adev);
436
437 if (acpi_match_device_ids(adev, wd->ids))
438 return 0;
439
440 if (!wd->uid)
441 goto match;
442
443 if (!uid)
444 /*
445 * Some DSDTs have only one ACPI companion for the two I2C
446 * controllers and they don't set a UID at all (e.g. Dell
447 * Latitude 9420). On these platforms only the first I2C
448 * controller is used, so if a HID match has no UID we use
449 * "0" as the UID and assign ACPI companion to the first
450 * I2C controller.
451 */
452 uid = "0";
453 else
454 uid = strchr(uid, wd->uid[0]);
455
456 if (!uid || strcmp(uid, wd->uid))
457 return 0;
458
459 match:
460 wd->adev = adev;
461
462 return 1;
463 }
464
465 /* bind auxiliary device to acpi device */
ljca_auxdev_acpi_bind(struct ljca_adapter * adap,struct auxiliary_device * auxdev,u64 adr,u8 id)466 static void ljca_auxdev_acpi_bind(struct ljca_adapter *adap,
467 struct auxiliary_device *auxdev,
468 u64 adr, u8 id)
469 {
470 struct ljca_match_ids_walk_data wd = { 0 };
471 struct device *dev = adap->dev;
472 struct acpi_device *parent;
473 char uid[4];
474
475 parent = ACPI_COMPANION(dev);
476 if (!parent)
477 return;
478
479 /*
480 * Currently LJCA hw doesn't use _ADR instead the shipped
481 * platforms use _HID to distinguish children devices.
482 */
483 switch (adr) {
484 case LJCA_GPIO_ACPI_ADR:
485 wd.ids = ljca_gpio_hids;
486 break;
487 case LJCA_I2C1_ACPI_ADR:
488 case LJCA_I2C2_ACPI_ADR:
489 snprintf(uid, sizeof(uid), "%d", id);
490 wd.uid = uid;
491 wd.ids = ljca_i2c_hids;
492 break;
493 case LJCA_SPI1_ACPI_ADR:
494 case LJCA_SPI2_ACPI_ADR:
495 wd.ids = ljca_spi_hids;
496 break;
497 default:
498 dev_warn(dev, "unsupported _ADR\n");
499 return;
500 }
501
502 acpi_dev_for_each_child(parent, ljca_match_device_ids, &wd);
503 if (wd.adev) {
504 ACPI_COMPANION_SET(&auxdev->dev, wd.adev);
505 return;
506 }
507
508 parent = ACPI_COMPANION(dev->parent->parent);
509 if (!parent)
510 return;
511
512 acpi_dev_for_each_child(parent, ljca_match_device_ids, &wd);
513 if (wd.adev)
514 ACPI_COMPANION_SET(&auxdev->dev, wd.adev);
515 }
516
ljca_auxdev_release(struct device * dev)517 static void ljca_auxdev_release(struct device *dev)
518 {
519 struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
520
521 kfree(auxdev->dev.platform_data);
522 }
523
ljca_new_client_device(struct ljca_adapter * adap,u8 type,u8 id,char * name,void * data,u64 adr)524 static int ljca_new_client_device(struct ljca_adapter *adap, u8 type, u8 id,
525 char *name, void *data, u64 adr)
526 {
527 struct auxiliary_device *auxdev;
528 struct ljca_client *client;
529 int ret;
530
531 client = kzalloc_obj(*client);
532 if (!client) {
533 kfree(data);
534 return -ENOMEM;
535 }
536
537 client->type = type;
538 client->id = id;
539 client->adapter = adap;
540 spin_lock_init(&client->event_cb_lock);
541
542 auxdev = &client->auxdev;
543 auxdev->name = name;
544 auxdev->id = id;
545
546 auxdev->dev.parent = adap->dev;
547 auxdev->dev.platform_data = data;
548 auxdev->dev.release = ljca_auxdev_release;
549
550 ret = auxiliary_device_init(auxdev);
551 if (ret) {
552 kfree(data);
553 goto err_free;
554 }
555
556 ljca_auxdev_acpi_bind(adap, auxdev, adr, id);
557
558 ret = auxiliary_device_add(auxdev);
559 if (ret)
560 goto err_uninit;
561
562 list_add_tail(&client->link, &adap->client_list);
563
564 return 0;
565
566 err_uninit:
567 auxiliary_device_uninit(auxdev);
568
569 err_free:
570 kfree(client);
571
572 return ret;
573 }
574
ljca_enumerate_gpio(struct ljca_adapter * adap)575 static int ljca_enumerate_gpio(struct ljca_adapter *adap)
576 {
577 u32 valid_pin[LJCA_MAX_GPIO_NUM / BITS_PER_TYPE(u32)];
578 struct ljca_gpio_descriptor *desc;
579 struct ljca_gpio_info *gpio_info;
580 u8 buf[LJCA_MAX_PAYLOAD_SIZE];
581 int ret, gpio_num;
582 unsigned int i;
583
584 ret = ljca_send(adap, LJCA_CLIENT_MNG, LJCA_MNG_ENUM_GPIO, NULL, 0, buf,
585 sizeof(buf), true, LJCA_ENUM_CLIENT_TIMEOUT_MS);
586 if (ret < 0)
587 return ret;
588
589 /* check firmware response */
590 desc = (struct ljca_gpio_descriptor *)buf;
591 if (ret != struct_size(desc, bank_desc, desc->bank_num))
592 return -EINVAL;
593
594 gpio_num = desc->pins_per_bank * desc->bank_num;
595 if (gpio_num > LJCA_MAX_GPIO_NUM)
596 return -EINVAL;
597
598 if (desc->bank_num > ARRAY_SIZE(valid_pin))
599 return -EINVAL;
600
601 /* construct platform data */
602 gpio_info = kzalloc_obj(*gpio_info);
603 if (!gpio_info)
604 return -ENOMEM;
605 gpio_info->num = gpio_num;
606
607 for (i = 0; i < desc->bank_num; i++)
608 valid_pin[i] = get_unaligned_le32(&desc->bank_desc[i].valid_pins);
609 bitmap_from_arr32(gpio_info->valid_pin_map, valid_pin, gpio_num);
610
611 return ljca_new_client_device(adap, LJCA_CLIENT_GPIO, 0, "ljca-gpio",
612 gpio_info, LJCA_GPIO_ACPI_ADR);
613 }
614
ljca_enumerate_i2c(struct ljca_adapter * adap)615 static int ljca_enumerate_i2c(struct ljca_adapter *adap)
616 {
617 struct ljca_i2c_descriptor *desc;
618 struct ljca_i2c_info *i2c_info;
619 u8 buf[LJCA_MAX_PAYLOAD_SIZE];
620 unsigned int i;
621 int ret;
622
623 ret = ljca_send(adap, LJCA_CLIENT_MNG, LJCA_MNG_ENUM_I2C, NULL, 0, buf,
624 sizeof(buf), true, LJCA_ENUM_CLIENT_TIMEOUT_MS);
625 if (ret < 0)
626 return ret;
627
628 /* check firmware response */
629 desc = (struct ljca_i2c_descriptor *)buf;
630 if (ret != struct_size(desc, info, desc->num))
631 return -EINVAL;
632
633 for (i = 0; i < desc->num; i++) {
634 /* construct platform data */
635 i2c_info = kzalloc_obj(*i2c_info);
636 if (!i2c_info)
637 return -ENOMEM;
638
639 i2c_info->id = desc->info[i].id;
640 i2c_info->capacity = desc->info[i].capacity;
641 i2c_info->intr_pin = desc->info[i].intr_pin;
642
643 ret = ljca_new_client_device(adap, LJCA_CLIENT_I2C, i,
644 "ljca-i2c", i2c_info,
645 LJCA_I2C1_ACPI_ADR + i);
646 if (ret)
647 return ret;
648 }
649
650 return 0;
651 }
652
ljca_enumerate_spi(struct ljca_adapter * adap)653 static int ljca_enumerate_spi(struct ljca_adapter *adap)
654 {
655 struct ljca_spi_descriptor *desc;
656 struct ljca_spi_info *spi_info;
657 u8 buf[LJCA_MAX_PAYLOAD_SIZE];
658 unsigned int i;
659 int ret;
660
661 /* Not all LJCA chips implement SPI, a timeout reading the descriptors is normal */
662 ret = ljca_send(adap, LJCA_CLIENT_MNG, LJCA_MNG_ENUM_SPI, NULL, 0, buf,
663 sizeof(buf), true, LJCA_ENUM_CLIENT_TIMEOUT_MS);
664 if (ret < 0)
665 return (ret == -ETIMEDOUT) ? 0 : ret;
666
667 /* check firmware response */
668 desc = (struct ljca_spi_descriptor *)buf;
669 if (ret != struct_size(desc, info, desc->num))
670 return -EINVAL;
671
672 for (i = 0; i < desc->num; i++) {
673 /* construct platform data */
674 spi_info = kzalloc_obj(*spi_info);
675 if (!spi_info)
676 return -ENOMEM;
677
678 spi_info->id = desc->info[i].id;
679 spi_info->capacity = desc->info[i].capacity;
680
681 ret = ljca_new_client_device(adap, LJCA_CLIENT_SPI, i,
682 "ljca-spi", spi_info,
683 LJCA_SPI1_ACPI_ADR + i);
684 if (ret)
685 return ret;
686 }
687
688 return 0;
689 }
690
ljca_reset_handshake(struct ljca_adapter * adap)691 static int ljca_reset_handshake(struct ljca_adapter *adap)
692 {
693 __le32 reset_id = cpu_to_le32(adap->reset_id);
694 __le32 reset_id_ret = 0;
695 int ret;
696
697 adap->reset_id++;
698
699 ret = ljca_send(adap, LJCA_CLIENT_MNG, LJCA_MNG_RESET, (u8 *)&reset_id,
700 sizeof(__le32), (u8 *)&reset_id_ret, sizeof(__le32),
701 true, LJCA_WRITE_ACK_TIMEOUT_MS);
702 if (ret < 0)
703 return ret;
704
705 if (reset_id_ret != reset_id)
706 return -EINVAL;
707
708 return 0;
709 }
710
ljca_enumerate_clients(struct ljca_adapter * adap)711 static int ljca_enumerate_clients(struct ljca_adapter *adap)
712 {
713 struct ljca_client *client, *next;
714 int ret;
715
716 ret = ljca_reset_handshake(adap);
717 if (ret)
718 goto err_kill;
719
720 ret = ljca_enumerate_gpio(adap);
721 if (ret) {
722 dev_err(adap->dev, "enumerate GPIO error\n");
723 goto err_kill;
724 }
725
726 ret = ljca_enumerate_i2c(adap);
727 if (ret) {
728 dev_err(adap->dev, "enumerate I2C error\n");
729 goto err_kill;
730 }
731
732 ret = ljca_enumerate_spi(adap);
733 if (ret) {
734 dev_err(adap->dev, "enumerate SPI error\n");
735 goto err_kill;
736 }
737
738 return 0;
739
740 err_kill:
741 adap->disconnect = true;
742
743 usb_kill_urb(adap->rx_urb);
744
745 list_for_each_entry_safe_reverse(client, next, &adap->client_list, link) {
746 auxiliary_device_delete(&client->auxdev);
747 auxiliary_device_uninit(&client->auxdev);
748
749 list_del_init(&client->link);
750 kfree(client);
751 }
752
753 return ret;
754 }
755
ljca_probe(struct usb_interface * interface,const struct usb_device_id * id)756 static int ljca_probe(struct usb_interface *interface,
757 const struct usb_device_id *id)
758 {
759 struct usb_device *usb_dev = interface_to_usbdev(interface);
760 struct usb_host_interface *alt = interface->cur_altsetting;
761 struct usb_endpoint_descriptor *ep_in, *ep_out;
762 struct device *dev = &interface->dev;
763 struct ljca_adapter *adap;
764 int ret;
765
766 adap = devm_kzalloc(dev, sizeof(*adap), GFP_KERNEL);
767 if (!adap)
768 return -ENOMEM;
769
770 /* separate tx buffer allocation for alignment */
771 adap->tx_buf = devm_kzalloc(dev, LJCA_MAX_PACKET_SIZE, GFP_KERNEL);
772 if (!adap->tx_buf)
773 return -ENOMEM;
774 adap->tx_buf_len = LJCA_MAX_PACKET_SIZE;
775
776 mutex_init(&adap->mutex);
777 spin_lock_init(&adap->lock);
778 init_completion(&adap->cmd_completion);
779 INIT_LIST_HEAD(&adap->client_list);
780
781 adap->intf = interface;
782 adap->usb_dev = usb_dev;
783 adap->dev = dev;
784
785 /*
786 * find the first bulk in and out endpoints.
787 * ignore any others.
788 */
789 ret = usb_find_common_endpoints(alt, &ep_in, &ep_out, NULL, NULL);
790 if (ret) {
791 dev_err(dev, "bulk endpoints not found\n");
792 goto err_destroy_mutex;
793 }
794 adap->rx_pipe = usb_rcvbulkpipe(usb_dev, usb_endpoint_num(ep_in));
795 adap->tx_pipe = usb_sndbulkpipe(usb_dev, usb_endpoint_num(ep_out));
796
797 /* setup rx buffer */
798 adap->rx_len = usb_endpoint_maxp(ep_in);
799 adap->rx_buf = devm_kzalloc(dev, adap->rx_len, GFP_KERNEL);
800 if (!adap->rx_buf) {
801 ret = -ENOMEM;
802 goto err_destroy_mutex;
803 }
804
805 /* alloc rx urb */
806 adap->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
807 if (!adap->rx_urb) {
808 ret = -ENOMEM;
809 goto err_destroy_mutex;
810 }
811 usb_fill_bulk_urb(adap->rx_urb, usb_dev, adap->rx_pipe,
812 adap->rx_buf, adap->rx_len, ljca_recv, adap);
813
814 usb_set_intfdata(interface, adap);
815
816 /* submit rx urb before enumerate clients */
817 ret = usb_submit_urb(adap->rx_urb, GFP_KERNEL);
818 if (ret) {
819 dev_err(dev, "submit rx urb failed: %d\n", ret);
820 goto err_free;
821 }
822
823 ret = ljca_enumerate_clients(adap);
824 if (ret)
825 goto err_free;
826
827 /*
828 * This works around problems with ov2740 initialization on some
829 * Lenovo platforms. The autosuspend delay, has to be smaller than
830 * the delay after setting the reset_gpio line in ov2740_resume().
831 * Otherwise the sensor randomly fails to initialize.
832 */
833 pm_runtime_set_autosuspend_delay(&usb_dev->dev, 10);
834
835 usb_enable_autosuspend(usb_dev);
836
837 return 0;
838
839 err_free:
840 usb_free_urb(adap->rx_urb);
841 err_destroy_mutex:
842 mutex_destroy(&adap->mutex);
843
844 return ret;
845 }
846
ljca_disconnect(struct usb_interface * interface)847 static void ljca_disconnect(struct usb_interface *interface)
848 {
849 struct ljca_adapter *adap = usb_get_intfdata(interface);
850 struct ljca_client *client, *next;
851
852 adap->disconnect = true;
853
854 usb_kill_urb(adap->rx_urb);
855
856 list_for_each_entry_safe_reverse(client, next, &adap->client_list, link) {
857 auxiliary_device_delete(&client->auxdev);
858 auxiliary_device_uninit(&client->auxdev);
859
860 list_del_init(&client->link);
861 kfree(client);
862 }
863
864 usb_free_urb(adap->rx_urb);
865
866 mutex_destroy(&adap->mutex);
867 }
868
ljca_suspend(struct usb_interface * interface,pm_message_t message)869 static int ljca_suspend(struct usb_interface *interface, pm_message_t message)
870 {
871 struct ljca_adapter *adap = usb_get_intfdata(interface);
872
873 usb_kill_urb(adap->rx_urb);
874
875 return 0;
876 }
877
ljca_resume(struct usb_interface * interface)878 static int ljca_resume(struct usb_interface *interface)
879 {
880 struct ljca_adapter *adap = usb_get_intfdata(interface);
881
882 return usb_submit_urb(adap->rx_urb, GFP_KERNEL);
883 }
884
885 static const struct usb_device_id ljca_table[] = {
886 { USB_DEVICE(0x8086, 0x0b63) },
887 { /* sentinel */ }
888 };
889 MODULE_DEVICE_TABLE(usb, ljca_table);
890
891 static struct usb_driver ljca_driver = {
892 .name = "ljca",
893 .id_table = ljca_table,
894 .probe = ljca_probe,
895 .disconnect = ljca_disconnect,
896 .suspend = ljca_suspend,
897 .resume = ljca_resume,
898 .supports_autosuspend = 1,
899 };
900 module_usb_driver(ljca_driver);
901
902 MODULE_AUTHOR("Wentong Wu");
903 MODULE_AUTHOR("Zhifeng Wang <zhifeng.wang@intel.com>");
904 MODULE_AUTHOR("Lixu Zhang <lixu.zhang@intel.com>");
905 MODULE_DESCRIPTION("Intel La Jolla Cove Adapter USB driver");
906 MODULE_LICENSE("GPL");
907