dts: rk312x: default enable emmc
[firefly-linux-kernel-4.4.55.git] / drivers / video / rockchip / hdmi / chips / rk616 / rk616_hdcp.c
1 #include <linux/module.h>
2 #include <linux/kernel.h>
3 #include <linux/errno.h>
4 #include <linux/string.h>
5 #include <linux/miscdevice.h>
6 #include <linux/workqueue.h>
7 #include <linux/firmware.h>
8 #include "rk616_hdmi.h"
9 #include "rk616_hdcp.h"
10
11 struct hdcp *hdcp = NULL;
12
13 static void hdcp_work_queue(struct work_struct *work);
14
15 /*-----------------------------------------------------------------------------
16  * Function: hdcp_submit_work
17  *-----------------------------------------------------------------------------
18  */
19 static struct delayed_work *hdcp_submit_work(int event, int delay)
20 {
21         struct hdcp_delayed_work *work;
22
23         DBG("%s event %04x delay %d", __FUNCTION__, event, delay);
24         
25         work = kmalloc(sizeof(struct hdcp_delayed_work), GFP_ATOMIC);
26
27         if (work) {
28                 INIT_DELAYED_WORK(&work->work, hdcp_work_queue);
29                 work->event = event;
30                 queue_delayed_work(hdcp->workqueue,
31                                    &work->work,
32                                    msecs_to_jiffies(delay));
33         } else {
34                 printk(KERN_WARNING "HDCP: Cannot allocate memory to "
35                                     "create work\n");
36                 return 0;
37         }
38
39         return &work->work;
40 }
41
42 /*-----------------------------------------------------------------------------
43  * Function: hdcp_cancel_work
44  *-----------------------------------------------------------------------------
45  */
46 static void hdcp_cancel_work(struct delayed_work **work)
47 {
48         int ret = 0;
49
50         if (*work) {
51                 ret = cancel_delayed_work(*work);
52                 if (ret != 1) {
53                         ret = cancel_work_sync(&((*work)->work));
54                         printk(KERN_INFO "Canceling work failed - "
55                                          "cancel_work_sync done %d\n", ret);
56                 }
57                 kfree(*work);
58                 *work = 0;
59         }
60 }
61
62 /*-----------------------------------------------------------------------------
63  * Function: hdcp_wq_authentication_failure
64  *-----------------------------------------------------------------------------
65  */
66 static void hdcp_wq_authentication_failure(void)
67 {
68         if (hdcp->hdmi_state == HDMI_STOPPED) {
69                 return;
70         }
71
72         rk616_hdcp_disable();
73         rk616_hdmi_control_output(false);
74         
75         hdcp_cancel_work(&hdcp->pending_wq_event);
76         
77         if (hdcp->retry_cnt && (hdcp->hdmi_state != HDMI_STOPPED)) {
78                 if (hdcp->retry_cnt < HDCP_INFINITE_REAUTH) {
79                         hdcp->retry_cnt--;
80                         printk(KERN_INFO "HDCP: authentication failed - "
81                                          "retrying, attempts=%d\n",
82                                                         hdcp->retry_cnt);
83                 } else
84                         printk(KERN_INFO "HDCP: authentication failed - "
85                                          "retrying\n");
86
87                 hdcp->hdcp_state = HDCP_AUTHENTICATION_START;
88
89                 hdcp->pending_wq_event = hdcp_submit_work(HDCP_AUTH_REATT_EVENT,
90                                                          HDCP_REAUTH_DELAY);
91         } else {
92                 printk(KERN_INFO "HDCP: authentication failed - "
93                                  "HDCP disabled\n");
94                 hdcp->hdcp_state = HDCP_ENABLE_PENDING;
95         }
96
97 }
98
99 /*-----------------------------------------------------------------------------
100  * Function: hdcp_wq_start_authentication
101  *-----------------------------------------------------------------------------
102  */
103 static void hdcp_wq_start_authentication(void)
104 {
105         int status = HDCP_OK;
106
107         hdcp->hdcp_state = HDCP_AUTHENTICATION_START;
108
109         DBG("HDCP: authentication start");
110
111         status = rk616_hdcp_start_authentication();
112
113         if (status != HDCP_OK) {
114                 DBG("HDCP: authentication failed");
115                 hdcp_wq_authentication_failure();
116         } else {
117                 hdcp->hdcp_state = HDCP_WAIT_KSV_LIST;
118 //              hdcp->hdcp_state = HDCP_LINK_INTEGRITY_CHECK;
119         }
120 }
121
122 /*-----------------------------------------------------------------------------
123  * Function: hdcp_wq_check_bksv
124  *-----------------------------------------------------------------------------
125  */
126 static void hdcp_wq_check_bksv(void)
127 {
128         int status = HDCP_OK;
129
130         DBG("Check BKSV start");
131         
132         status = rk616_hdcp_check_bksv();
133
134         if (status != HDCP_OK) {
135                 printk(KERN_INFO "HDCP: Check BKSV failed");
136                 hdcp->retry_cnt = 0;
137                 hdcp_wq_authentication_failure();
138         }
139         else {
140                 DBG("HDCP: Check BKSV successful");
141
142                 hdcp->hdcp_state = HDCP_LINK_INTEGRITY_CHECK;
143
144                 /* Restore retry counter */
145                 if(hdcp->retry_times == 0)
146                         hdcp->retry_cnt = HDCP_INFINITE_REAUTH;
147                 else
148                         hdcp->retry_cnt = hdcp->retry_times;
149         }
150 }
151
152 /*-----------------------------------------------------------------------------
153  * Function: hdcp_wq_authentication_sucess
154  *-----------------------------------------------------------------------------
155  */
156 static void hdcp_wq_authentication_sucess(void)
157 {
158         rk616_hdmi_control_output(true);
159         printk(KERN_INFO "HDCP: authentication pass");
160 }
161
162 /*-----------------------------------------------------------------------------
163  * Function: hdcp_wq_disable
164  *-----------------------------------------------------------------------------
165  */
166 static void hdcp_wq_disable(int event)
167 {
168         printk(KERN_INFO "HDCP: disabled");
169
170         hdcp_cancel_work(&hdcp->pending_wq_event);
171         rk616_hdcp_disable();
172         if(event == HDCP_DISABLE_CTL) {
173                 hdcp->hdcp_state = HDCP_DISABLED;
174                 if(hdcp->hdmi_state == HDMI_STARTED)
175                         rk616_hdmi_control_output(true);
176         }
177         else if(event == HDCP_STOP_FRAME_EVENT)
178                 hdcp->hdcp_state = HDCP_ENABLE_PENDING;
179 }
180
181 /*-----------------------------------------------------------------------------
182  * Function: hdcp_work_queue
183  *-----------------------------------------------------------------------------
184  */
185 static void hdcp_work_queue(struct work_struct *work)
186 {
187         struct hdcp_delayed_work *hdcp_w =
188                 container_of(work, struct hdcp_delayed_work, work.work);
189         int event = hdcp_w->event;
190
191         mutex_lock(&hdcp->lock);
192         
193         DBG("hdcp_work_queue() - START - %u hdmi=%d hdcp=%d evt= %x %d",
194                 jiffies_to_msecs(jiffies),
195                 hdcp->hdmi_state,
196                 hdcp->hdcp_state,
197                 (event & 0xFF00) >> 8,
198                 event & 0xFF);
199         
200         if(event == HDCP_STOP_FRAME_EVENT) {
201                 hdcp->hdmi_state = HDMI_STOPPED;
202         }
203         
204         if (event == HDCP_DISABLE_CTL || event == HDCP_STOP_FRAME_EVENT) {
205                 hdcp_wq_disable(event);
206         }
207         
208         if (event & HDCP_WORKQUEUE_SRC)
209                 hdcp->pending_wq_event = 0;
210         
211         /* First handle HDMI state */
212         if (event == HDCP_START_FRAME_EVENT) {
213                 hdcp->pending_start = 0;
214                 hdcp->hdmi_state = HDMI_STARTED;
215         }
216         
217         /**********************/
218         /* HDCP state machine */
219         /**********************/
220         switch (hdcp->hdcp_state) {
221                 case HDCP_DISABLED:
222                         /* HDCP enable control or re-authentication event */
223                         if (event == HDCP_ENABLE_CTL) {
224                                 if(hdcp->retry_times == 0)
225                                         hdcp->retry_cnt = HDCP_INFINITE_REAUTH;
226                                 else
227                                         hdcp->retry_cnt = hdcp->retry_times;
228                                 if (hdcp->hdmi_state == HDMI_STARTED)
229                                         hdcp_wq_start_authentication();
230                                 else
231                                         hdcp->hdcp_state = HDCP_ENABLE_PENDING;
232                         }
233                         break;
234                 
235                 case HDCP_ENABLE_PENDING:
236                         /* HDMI start frame event */
237                         if (event == HDCP_START_FRAME_EVENT)
238                                 hdcp_wq_start_authentication();
239
240                         break;
241                 
242                 case HDCP_AUTHENTICATION_START:
243                         /* Re-authentication */
244                         if (event == HDCP_AUTH_REATT_EVENT)
245                                 hdcp_wq_start_authentication();
246         
247                         break;
248                 
249                 case HDCP_WAIT_KSV_LIST:
250                         /* KSV failure */
251                         if (event == HDCP_FAIL_EVENT) {
252                                 printk(KERN_INFO "HDCP: KSV switch failure\n");
253         
254                                 hdcp_wq_authentication_failure();
255                         }
256                         /* KSV list ready event */
257                         else if (event == HDCP_KSV_LIST_RDY_EVENT)
258                                 hdcp_wq_check_bksv();
259                         break;
260                 
261                 case HDCP_LINK_INTEGRITY_CHECK:
262                         /* Ri failure */
263                         if (event == HDCP_FAIL_EVENT) {
264                                 printk(KERN_INFO "HDCP: Ri check failure\n");
265                                 hdcp_wq_authentication_failure();
266                         }
267                         else if(event == HDCP_AUTH_PASS_EVENT)
268                                 hdcp_wq_authentication_sucess();
269                         break;
270         
271                 default:
272                         printk(KERN_WARNING "HDCP: error - unknow HDCP state\n");
273                         break;
274         }
275         
276         kfree(hdcp_w);
277         if(event == HDCP_STOP_FRAME_EVENT)
278                 complete(&hdcp->complete);
279                 
280         mutex_unlock(&hdcp->lock);
281 }
282
283 /*-----------------------------------------------------------------------------
284  * Function: hdcp_start_frame_cb
285  *-----------------------------------------------------------------------------
286  */
287 static void hdcp_start_frame_cb(void)
288 {
289         DBG("hdcp_start_frame_cb()");
290
291         /* Cancel any pending work */
292         if (hdcp->pending_start)
293                 hdcp_cancel_work(&hdcp->pending_start);
294         if (hdcp->pending_wq_event)
295                 hdcp_cancel_work(&hdcp->pending_wq_event);
296
297         hdcp->pending_start = hdcp_submit_work(HDCP_START_FRAME_EVENT,
298                                                         HDCP_ENABLE_DELAY);
299 }
300
301 /*-----------------------------------------------------------------------------
302  * Function: hdcp_irq_cb
303  *-----------------------------------------------------------------------------
304  */
305 static void hdcp_irq_cb(int status)
306 {
307         char interrupt1;
308         char interrupt2;
309         
310         rk616_hdcp_interrupt(&interrupt1, &interrupt2);
311         DBG("%s 0x%02x 0x%02x", __FUNCTION__, interrupt1, interrupt2);
312         if(interrupt1 & m_INT_HDCP_ERR)
313         {
314                 if( (hdcp->hdcp_state != HDCP_DISABLED) &&
315                         (hdcp->hdcp_state != HDCP_ENABLE_PENDING) )
316                 {       
317                         hdcp_submit_work(HDCP_FAIL_EVENT, 0);
318                 }
319         }
320         else if(interrupt1 & (m_INT_BKSV_READY | m_INT_BKSV_UPDATE))
321                 hdcp_submit_work(HDCP_KSV_LIST_RDY_EVENT, 0);
322         else if(interrupt1 & m_INT_AUTH_SUCCESS)
323                 hdcp_submit_work(HDCP_AUTH_PASS_EVENT, 0);
324 }
325
326 /*-----------------------------------------------------------------------------
327  * Function: hdcp_power_on_cb
328  *-----------------------------------------------------------------------------
329  */
330 static int hdcp_power_on_cb(void)
331 {
332         DBG("%s", __FUNCTION__);
333 //      return rk616_hdcp_load_key2mem(hdcp->keys);
334         return HDCP_OK;
335 }
336
337 /*-----------------------------------------------------------------------------
338  * Function: hdcp_power_off_cb
339  *-----------------------------------------------------------------------------
340  */
341 static void hdcp_power_off_cb(void)
342 {
343         DBG("%s", __FUNCTION__);
344         if(!hdcp->enable)
345                 return;
346         
347         hdcp_cancel_work(&hdcp->pending_start);
348         hdcp_cancel_work(&hdcp->pending_wq_event);
349         init_completion(&hdcp->complete);
350         /* Post event to workqueue */
351         if (hdcp_submit_work(HDCP_STOP_FRAME_EVENT, 0)) 
352                 wait_for_completion_interruptible_timeout(&hdcp->complete,
353                                                         msecs_to_jiffies(5000));
354 }
355
356 // Load HDCP key to external HDCP memory
357 static void hdcp_load_keys_cb(const struct firmware *fw, void *context)
358 {
359         if (!fw) {
360                 pr_err("HDCP: failed to load keys\n");
361                 return;
362         }
363         
364         if(fw->size < HDCP_KEY_SIZE) {
365                 pr_err("HDCP: firmware wrong size %d\n", fw->size);
366                 return;
367         }
368         
369         hdcp->keys =  kmalloc(HDCP_KEY_SIZE, GFP_KERNEL);
370         if(hdcp->keys == NULL) {
371                 pr_err("HDCP: can't allocated space for keys\n");
372                 return;
373         }
374         
375         memcpy(hdcp->keys, fw->data, HDCP_KEY_SIZE);
376         
377         printk(KERN_INFO "HDCP: load hdcp key success\n");
378
379         if(fw->size > HDCP_KEY_SIZE) {
380                 DBG("%s invalid key size %d", __FUNCTION__, fw->size - HDCP_KEY_SIZE);
381                 if((fw->size - HDCP_KEY_SIZE) % 5) {
382                         pr_err("HDCP: failed to load invalid keys\n");
383                         return;
384                 }
385                 hdcp->invalidkeys = kmalloc(fw->size - HDCP_KEY_SIZE, GFP_KERNEL);
386                 if(hdcp->invalidkeys == NULL) {
387                         pr_err("HDCP: can't allocated space for invalid keys\n");
388                         return;
389                 }
390                 memcpy(hdcp->invalidkeys, fw->data + HDCP_KEY_SIZE, fw->size - HDCP_KEY_SIZE);
391                 hdcp->invalidkey = (fw->size - HDCP_KEY_SIZE)/5;
392                 printk(KERN_INFO "HDCP: loaded hdcp invalid key success\n");
393         }
394 }
395
396 static ssize_t hdcp_enable_read(struct device *device,
397                             struct device_attribute *attr, char *buf)
398 {
399         int enable = 0;
400         
401         if(hdcp)
402                 enable = hdcp->enable;
403                 
404         return snprintf(buf, PAGE_SIZE, "%d\n", enable);
405 }
406
407 static ssize_t hdcp_enable_write(struct device *device,
408                            struct device_attribute *attr, const char *buf, size_t count)
409 {
410         int enable;
411
412         if(hdcp == NULL)
413                 return -EINVAL;
414         
415         sscanf(buf, "%d", &enable);
416         if(hdcp->enable != enable)
417         {
418                 /* Post event to workqueue */
419                 if(enable) {
420                         if (hdcp_submit_work(HDCP_ENABLE_CTL, 0) == 0)
421                                 return -EFAULT;
422                 }
423                 else {
424                         hdcp_cancel_work(&hdcp->pending_start);
425                         hdcp_cancel_work(&hdcp->pending_wq_event);
426                 
427                         /* Post event to workqueue */
428                         if (hdcp_submit_work(HDCP_DISABLE_CTL, 0) == 0)
429                                 return -EFAULT;
430                 }
431                 hdcp->enable =  enable;
432         }
433         return count;
434 }
435
436 static DEVICE_ATTR(enable, S_IRUGO|S_IWUSR, hdcp_enable_read, hdcp_enable_write);
437
438 static ssize_t hdcp_trytimes_read(struct device *device,
439                             struct device_attribute *attr, char *buf)
440 {
441         int trytimes = 0;
442         
443         if(hdcp)
444                 trytimes = hdcp->retry_times;
445                 
446         return snprintf(buf, PAGE_SIZE, "%d\n", trytimes);
447 }
448
449 static ssize_t hdcp_trytimes_wrtie(struct device *device,
450                            struct device_attribute *attr, const char *buf, size_t count)
451 {
452         int trytimes;
453
454         if(hdcp == NULL)
455                 return -EINVAL;
456         
457         sscanf(buf, "%d", &trytimes);
458         if(hdcp->retry_times != trytimes)
459                 hdcp->retry_times = trytimes;
460         
461         return count;
462 }
463
464
465 static DEVICE_ATTR(trytimes, S_IRUGO|S_IWUSR, hdcp_trytimes_read, hdcp_trytimes_wrtie);
466
467
468 static struct miscdevice mdev;
469
470 static int __init rk616_hdcp_init(void)
471 {
472         int ret;
473         
474         DBG("[%s] %u", __FUNCTION__, jiffies_to_msecs(jiffies));
475         
476         hdcp = kmalloc(sizeof(struct hdcp), GFP_KERNEL);
477         if(!hdcp)
478         {
479         printk(KERN_ERR ">>HDCP: kmalloc fail!");
480         ret = -ENOMEM;
481         goto error0; 
482         }
483         memset(hdcp, 0, sizeof(struct hdcp));
484         mutex_init(&hdcp->lock);
485         
486         mdev.minor = MISC_DYNAMIC_MINOR;
487         mdev.name = "hdcp";
488         mdev.mode = 0666;
489         if (misc_register(&mdev)) {
490                 printk(KERN_ERR "HDCP: Could not add character driver\n");
491                 ret = HDMI_ERROR_FALSE;
492                 goto error1;
493         }
494         ret = device_create_file(mdev.this_device, &dev_attr_enable);
495     if(ret)
496     {
497         printk(KERN_ERR "HDCP: Could not add sys file enable\n");
498         ret = -EINVAL;
499         goto error2;
500     }
501     
502     ret = device_create_file(mdev.this_device, &dev_attr_trytimes);
503     if(ret)
504     {
505         printk(KERN_ERR "HDCP: Could not add sys file trytimes\n");
506         ret = -EINVAL;
507         goto error3;
508     }
509     
510     hdcp->workqueue = create_singlethread_workqueue("hdcp");
511         if (hdcp->workqueue == NULL) {
512                 printk(KERN_ERR "HDCP,: create workqueue failed.\n");
513                 goto error4;
514         }
515     
516     
517     ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG,
518                               "hdcp.keys", mdev.this_device, GFP_KERNEL,
519                               hdcp, hdcp_load_keys_cb);
520         if (ret < 0) {
521                 printk(KERN_ERR "HDCP: request_firmware_nowait failed: %d\n", ret);
522                 goto error5;
523         }
524         
525         rk616_hdmi_register_hdcp_callbacks(hdcp_start_frame_cb,
526                                                                                 hdcp_irq_cb,
527                                                                                 hdcp_power_on_cb,
528                                                                                 hdcp_power_off_cb);
529                                                                                 
530         DBG("%s success %u", __FUNCTION__, jiffies_to_msecs(jiffies));
531         return 0;
532         
533 error5:
534         destroy_workqueue(hdcp->workqueue);
535 error4:
536         device_remove_file(mdev.this_device, &dev_attr_trytimes);
537 error3:
538         device_remove_file(mdev.this_device, &dev_attr_enable);
539 error2:
540         misc_deregister(&mdev);
541 error1:
542         if(hdcp->keys)
543                 kfree(hdcp->keys);
544         if(hdcp->invalidkeys)
545                 kfree(hdcp->invalidkeys);
546         kfree(hdcp);
547 error0:
548         return ret;
549 }
550
551 static void __exit rk616_hdcp_exit(void)
552 {
553         device_remove_file(mdev.this_device, &dev_attr_enable);
554         misc_deregister(&mdev);
555         if(hdcp->keys)
556                 kfree(hdcp->keys);
557         if(hdcp->invalidkeys)
558                 kfree(hdcp->invalidkeys);
559         kfree(hdcp);
560 }
561
562 module_init(rk616_hdcp_init);
563 module_exit(rk616_hdcp_exit);