2 * Copyright (c) 2015, Sony Mobile Communications Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 #include <linux/firmware.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/soc/qcom/smd.h>
18 #define WCNSS_REQUEST_TIMEOUT (5 * HZ)
20 #define NV_FRAGMENT_SIZE 3072
21 #define NVBIN_FILE "wlan/prima/WCNSS_qcom_wlan_nv.bin"
24 * struct wcnss_ctrl - driver context
26 * @channel: SMD channel handle
27 * @ack: completion for outstanding requests
28 * @ack_status: status of the outstanding request
29 * @download_nv_work: worker for uploading nv binary
33 struct qcom_smd_channel
*channel
;
35 struct completion ack
;
38 struct work_struct download_nv_work
;
43 WCNSS_VERSION_REQ
= 0x01000000,
45 WCNSS_DOWNLOAD_NV_REQ
,
46 WCNSS_DOWNLOAD_NV_RESP
,
48 WCNSS_UPLOAD_CAL_RESP
,
49 WCNSS_DOWNLOAD_CAL_REQ
,
50 WCNSS_DOWNLOAD_CAL_RESP
,
54 * struct wcnss_msg_hdr - common packet header for requests and responses
55 * @type: packet message type
56 * @len: total length of the packet, including this header
58 struct wcnss_msg_hdr
{
64 * struct wcnss_version_resp - version request response
65 * @hdr: common packet wcnss_msg_hdr header
67 struct wcnss_version_resp
{
68 struct wcnss_msg_hdr hdr
;
76 * struct wcnss_download_nv_req - firmware fragment request
77 * @hdr: common packet wcnss_msg_hdr header
78 * @seq: sequence number of this fragment
79 * @last: boolean indicator of this being the last fragment of the binary
80 * @frag_size: length of this fragment
81 * @fragment: fragment data
83 struct wcnss_download_nv_req
{
84 struct wcnss_msg_hdr hdr
;
92 * struct wcnss_download_nv_resp - firmware download response
93 * @hdr: common packet wcnss_msg_hdr header
94 * @status: boolean to indicate success of the download
96 struct wcnss_download_nv_resp
{
97 struct wcnss_msg_hdr hdr
;
102 * wcnss_ctrl_smd_callback() - handler from SMD responses
103 * @qsdev: smd device handle
104 * @data: pointer to the incoming data packet
105 * @count: size of the incoming data packet
107 * Handles any incoming packets from the remote WCNSS_CTRL service.
109 static int wcnss_ctrl_smd_callback(struct qcom_smd_device
*qsdev
,
113 struct wcnss_ctrl
*wcnss
= dev_get_drvdata(&qsdev
->dev
);
114 const struct wcnss_download_nv_resp
*nvresp
;
115 const struct wcnss_version_resp
*version
;
116 const struct wcnss_msg_hdr
*hdr
= data
;
119 case WCNSS_VERSION_RESP
:
120 if (count
!= sizeof(*version
)) {
122 "invalid size of version response\n");
127 dev_info(wcnss
->dev
, "WCNSS Version %d.%d %d.%d\n",
128 version
->major
, version
->minor
,
129 version
->version
, version
->revision
);
131 schedule_work(&wcnss
->download_nv_work
);
133 case WCNSS_DOWNLOAD_NV_RESP
:
134 if (count
!= sizeof(*nvresp
)) {
136 "invalid size of download response\n");
141 wcnss
->ack_status
= nvresp
->status
;
142 complete(&wcnss
->ack
);
145 dev_info(wcnss
->dev
, "unknown message type %d\n", hdr
->type
);
153 * wcnss_request_version() - send a version request to WCNSS
154 * @wcnss: wcnss ctrl driver context
156 static int wcnss_request_version(struct wcnss_ctrl
*wcnss
)
158 struct wcnss_msg_hdr msg
;
160 msg
.type
= WCNSS_VERSION_REQ
;
161 msg
.len
= sizeof(msg
);
163 return qcom_smd_send(wcnss
->channel
, &msg
, sizeof(msg
));
167 * wcnss_download_nv() - send nv binary to WCNSS
168 * @work: work struct to acquire wcnss context
170 static void wcnss_download_nv(struct work_struct
*work
)
172 struct wcnss_ctrl
*wcnss
= container_of(work
, struct wcnss_ctrl
, download_nv_work
);
173 struct wcnss_download_nv_req
*req
;
174 const struct firmware
*fw
;
179 req
= kzalloc(sizeof(*req
) + NV_FRAGMENT_SIZE
, GFP_KERNEL
);
183 ret
= request_firmware(&fw
, NVBIN_FILE
, wcnss
->dev
);
185 dev_err(wcnss
->dev
, "Failed to load nv file %s: %d\n",
193 req
->hdr
.type
= WCNSS_DOWNLOAD_NV_REQ
;
194 req
->hdr
.len
= sizeof(*req
) + NV_FRAGMENT_SIZE
;
197 req
->frag_size
= NV_FRAGMENT_SIZE
;
201 if (left
<= NV_FRAGMENT_SIZE
) {
203 req
->frag_size
= left
;
204 req
->hdr
.len
= sizeof(*req
) + left
;
207 memcpy(req
->fragment
, data
, req
->frag_size
);
209 ret
= qcom_smd_send(wcnss
->channel
, req
, req
->hdr
.len
);
211 dev_err(wcnss
->dev
, "failed to send smd packet\n");
215 /* Increment for next fragment */
218 data
+= req
->hdr
.len
;
219 left
-= NV_FRAGMENT_SIZE
;
222 ret
= wait_for_completion_timeout(&wcnss
->ack
, WCNSS_REQUEST_TIMEOUT
);
224 dev_err(wcnss
->dev
, "timeout waiting for nv upload ack\n");
225 else if (wcnss
->ack_status
!= 1)
226 dev_err(wcnss
->dev
, "nv upload response failed err: %d\n",
230 release_firmware(fw
);
235 static int wcnss_ctrl_probe(struct qcom_smd_device
*sdev
)
237 struct wcnss_ctrl
*wcnss
;
239 wcnss
= devm_kzalloc(&sdev
->dev
, sizeof(*wcnss
), GFP_KERNEL
);
243 wcnss
->dev
= &sdev
->dev
;
244 wcnss
->channel
= sdev
->channel
;
246 init_completion(&wcnss
->ack
);
247 INIT_WORK(&wcnss
->download_nv_work
, wcnss_download_nv
);
249 dev_set_drvdata(&sdev
->dev
, wcnss
);
251 return wcnss_request_version(wcnss
);
254 static const struct qcom_smd_id wcnss_ctrl_smd_match
[] = {
255 { .name
= "WCNSS_CTRL" },
259 static struct qcom_smd_driver wcnss_ctrl_driver
= {
260 .probe
= wcnss_ctrl_probe
,
261 .callback
= wcnss_ctrl_smd_callback
,
262 .smd_match_table
= wcnss_ctrl_smd_match
,
264 .name
= "qcom_wcnss_ctrl",
265 .owner
= THIS_MODULE
,
269 module_qcom_smd_driver(wcnss_ctrl_driver
);
271 MODULE_DESCRIPTION("Qualcomm WCNSS control driver");
272 MODULE_LICENSE("GPL v2");