1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2021 pureLiFi
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/errno.h>
8 
9 #include "chip.h"
10 #include "mac.h"
11 #include "usb.h"
12 
plfxlc_chip_init(struct plfxlc_chip * chip,struct ieee80211_hw * hw,struct usb_interface * intf)13 void plfxlc_chip_init(struct plfxlc_chip *chip,
14 		      struct ieee80211_hw *hw,
15 		      struct usb_interface *intf)
16 {
17 	memset(chip, 0, sizeof(*chip));
18 	mutex_init(&chip->mutex);
19 	plfxlc_usb_init(&chip->usb, hw, intf);
20 }
21 
plfxlc_chip_release(struct plfxlc_chip * chip)22 void plfxlc_chip_release(struct plfxlc_chip *chip)
23 {
24 	plfxlc_usb_release(&chip->usb);
25 	mutex_destroy(&chip->mutex);
26 }
27 
plfxlc_set_beacon_interval(struct plfxlc_chip * chip,u16 interval,u8 dtim_period,int type)28 int plfxlc_set_beacon_interval(struct plfxlc_chip *chip, u16 interval,
29 			       u8 dtim_period, int type)
30 {
31 	if (!interval ||
32 	    (chip->beacon_set && chip->beacon_interval == interval))
33 		return 0;
34 
35 	chip->beacon_interval = interval;
36 	chip->beacon_set = true;
37 	return plfxlc_usb_wreq(chip->usb.ez_usb,
38 			       &chip->beacon_interval,
39 			       sizeof(chip->beacon_interval),
40 			       USB_REQ_BEACON_INTERVAL_WR);
41 }
42 
plfxlc_chip_init_hw(struct plfxlc_chip * chip)43 int plfxlc_chip_init_hw(struct plfxlc_chip *chip)
44 {
45 	unsigned char *addr = plfxlc_mac_get_perm_addr(plfxlc_chip_to_mac(chip));
46 	struct usb_device *udev = interface_to_usbdev(chip->usb.intf);
47 
48 	pr_info("plfxlc chip %04x:%04x v%02x %pM %s\n",
49 		le16_to_cpu(udev->descriptor.idVendor),
50 		le16_to_cpu(udev->descriptor.idProduct),
51 		le16_to_cpu(udev->descriptor.bcdDevice),
52 		addr,
53 		plfxlc_speed(udev->speed));
54 
55 	return plfxlc_set_beacon_interval(chip, 100, 0, 0);
56 }
57 
plfxlc_chip_switch_radio(struct plfxlc_chip * chip,u16 value)58 int plfxlc_chip_switch_radio(struct plfxlc_chip *chip, u16 value)
59 {
60 	int r;
61 	__le16 radio_on = cpu_to_le16(value);
62 
63 	r = plfxlc_usb_wreq(chip->usb.ez_usb, &radio_on,
64 			    sizeof(value), USB_REQ_POWER_WR);
65 	if (r)
66 		dev_err(plfxlc_chip_dev(chip), "POWER_WR failed (%d)\n", r);
67 	return r;
68 }
69 
plfxlc_chip_enable_rxtx(struct plfxlc_chip * chip)70 int plfxlc_chip_enable_rxtx(struct plfxlc_chip *chip)
71 {
72 	plfxlc_usb_enable_tx(&chip->usb);
73 	return plfxlc_usb_enable_rx(&chip->usb);
74 }
75 
plfxlc_chip_disable_rxtx(struct plfxlc_chip * chip)76 void plfxlc_chip_disable_rxtx(struct plfxlc_chip *chip)
77 {
78 	u8 value = 0;
79 
80 	plfxlc_usb_wreq(chip->usb.ez_usb,
81 			&value, sizeof(value), USB_REQ_RXTX_WR);
82 	plfxlc_usb_disable_rx(&chip->usb);
83 	plfxlc_usb_disable_tx(&chip->usb);
84 }
85 
plfxlc_chip_set_rate(struct plfxlc_chip * chip,u8 rate)86 int plfxlc_chip_set_rate(struct plfxlc_chip *chip, u8 rate)
87 {
88 	int r;
89 
90 	if (!chip)
91 		return -EINVAL;
92 
93 	r = plfxlc_usb_wreq(chip->usb.ez_usb,
94 			    &rate, sizeof(rate), USB_REQ_RATE_WR);
95 	if (r)
96 		dev_err(plfxlc_chip_dev(chip), "RATE_WR failed (%d)\n", r);
97 	return r;
98 }
99