aboutsummaryrefslogtreecommitdiff
path: root/drivers/misc/mediatek/secwidevine/classicdrm/secwidevine.c
blob: cb1221e3e4de30055543161ae845d8e3ecf7daff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428

#include <linux/kernel.h> 
#include <linux/module.h> 
#include <linux/init.h> 
#include <linux/moduleparam.h>
#include <linux/slab.h> 
#include <linux/unistd.h> 
#include <linux/sched.h> 
#include <linux/fs.h> 
#include <linux/uaccess.h> 
#include <linux/version.h>
#include <linux/spinlock.h>
#include <linux/semaphore.h>
#include <linux/delay.h>
#include <linux/kthread.h>
#include <linux/errno.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/mutex.h>
#include <linux/string.h>
#include <mach/memory.h>
#include <asm/io.h>
#include <linux/proc_fs.h>

/* only available for trustonic */
#include "mobicore_driver_api.h"

#include "secwidevine.h"

#define SECWIDEVINE_NAME     "secwidevine"
#define DEFAULT_HANDLES_NUM (64)
#define MAX_OPEN_SESSIONS   10

/* Debug message event */
#define DBG_EVT_NONE        (0)       /* No event */
#define DBG_EVT_CMD         (1 << 0)  /* SEC CMD related event */
#define DBG_EVT_FUNC        (1 << 1)  /* SEC function event */
#define DBG_EVT_INFO        (1 << 2)  /* SEC information event */
#define DBG_EVT_WRN         (1 << 30) /* Warning event */
#define DBG_EVT_ERR         (1 << 31) /* Error event */
#define DBG_EVT_ALL         (0xffffffff)

#define DBG_EVT_MASK        (DBG_EVT_ALL)

#define MSG(evt, fmt, args...) \
do {    \
    if ((DBG_EVT_##evt) & DBG_EVT_MASK) { \
        printk("[secwidevine][%s] "fmt, SECWIDEVINE_NAME, ##args); \
    }   \
} while(0)

#define MSG_FUNC() MSG(FUNC, "%s\n", __func__)

typedef struct mc_session_handle mc_session_handle_t;

/**
 * Overall TCI structure.
 */
#define TCI_PAYLOAD_LEN 1024
typedef struct {
    u8 message[TCI_PAYLOAD_LEN];   /**< TCI message */
} tci_t;

struct secwidevine_param {
    tci_t tci_data;
    u32 refcount;   /* INOUT */
    mc_session_handle_t session_handle; /* OUT */
};

static DEFINE_MUTEX(secwidevine_lock);

//This version just load secure driver
//#define TL_secwidevine_UUID {0x37, 0xb4, 0x04, 0xdb, 0x11, 0xdb, 0x4f, 0xd4, 0xaf, 0x5e, 0x84, 0x61, 0x06, 0xb1, 0xc3, 0xd0}
#define DR_secwidevine_UUID {0x37, 0x5d, 0xc3, 0xaa, 0x77, 0x68, 0x11, 0xe3, 0x8c, 0x99, 0x2c, 0x27, 0xd7, 0x44, 0xd6, 0x6e}
//static const struct mc_uuid_t secwidevine_uuid = {TL_secwidevine_UUID};
static const struct mc_uuid_t secwidevinedr_uuid = {DR_secwidevine_UUID};

static struct mc_session_handle secwidevinedr_session = {0};
static u32 secwidevine_session_ref = 0;
static u32 secwidevine_devid = MC_DEVICE_ID_DEFAULT;
static tci_t *secwidevine_tci = NULL;
static tci_t *secwidevinedr_tci = NULL;

static int secwidevine_execute(u32 cmd, struct secwidevine_param *param)
{

    mutex_lock(&secwidevine_lock);

    if (NULL == secwidevine_tci)
    {
        mutex_unlock(&secwidevine_lock);
        MSG(ERR, "secwidevine_tci not exist\n");
        return -ENODEV;
    }
    switch(cmd)
    {
    /*case CMD_SEC_WIDEVINE_GET_SESSION:
        param->session_handle = secwidevine_session;
        param->tci_data = *secwidevine_tci;
        param->refcount = secwidevine_session_ref;
        break;*/
    default:
        MSG(ERR, "Unkonw cmd\n");
        break;
    }

exit:

    mutex_unlock(&secwidevine_lock);

    return 0;
}

static int secwidevine_handle_register(struct secwidevine_context *ctx, u32 type, u32 id)
{
    return 0;
}

static void secwidevine_handle_unregister_check(struct secwidevine_context *ctx, u32 type, u32 id)
{

}

static int secwidevine_handle_unregister(struct secwidevine_context *ctx, u32 id)
{
    return 0;
}

static int secwidevine_handle_cleanup(struct secwidevine_context *ctx)
{
    return 0;
}

//Open driver in open
static int secwidevine_session_open(void)
{
    enum mc_result mc_ret = MC_DRV_OK;

    mutex_lock(&secwidevine_lock);

    do {
        /* sessions reach max numbers ? */
        if (secwidevine_session_ref > MAX_OPEN_SESSIONS)
        {
            MSG(WRN, "secwidevine_session > 0x%x\n", MAX_OPEN_SESSIONS);
            break;
        }

        if (secwidevine_session_ref > 0)
        {
            secwidevine_session_ref++;
            break;
        }
        
        /* open device */
        mc_ret = mc_open_device(secwidevine_devid);
        if (MC_DRV_OK != mc_ret)
        {
            MSG(ERR, "mc_open_device failed: %d\n", mc_ret);
            break;
        }

        /* allocating WSM for DCI */
        /*//open trustlet
        mc_ret = mc_malloc_wsm(secwidevine_devid, 0, sizeof(tci_t),
            (uint8_t **)&secwidevine_tci, 0);
        if (MC_DRV_OK != mc_ret) {
            mc_close_device(secwidevine_devid);
            MSG(ERR, "mc_malloc_wsm failed: %d\n", mc_ret);
            break;
        }

        // open session
        secwidevine_session.device_id = secwidevine_devid;
        mc_ret = mc_open_session(&secwidevine_session, &secwidevine_uuid,
            (uint8_t *)secwidevine_tci, sizeof(tci_t));

        if (MC_DRV_OK != mc_ret)
        {
            mc_free_wsm(secwidevine_devid, (uint8_t *)secwidevine_tci);
            secwidevine_tci = NULL;
            mc_close_device(secwidevine_devid);
            MSG(ERR, "mc_open_session failed: %d\n", mc_ret);
            break;
        }*/
        //open driver
        mc_ret = mc_malloc_wsm(secwidevine_devid, 0, sizeof(tci_t), (uint8_t **) &secwidevinedr_tci,
                0);
        if (MC_DRV_OK != mc_ret)
        {
            /*mc_free_wsm(secwidevine_devid, (uint8_t *) secwidevine_tci);
            secwidevine_tci = NULL;*/
            mc_close_device(secwidevine_devid);
            MSG(ERR, "2.mc_malloc_wsm failed: %d\n", mc_ret);
            break;
        }

        /* open session */
        secwidevinedr_session.device_id = secwidevine_devid;
        mc_ret = mc_open_session(&secwidevinedr_session, &secwidevinedr_uuid,
                (uint8_t *) secwidevinedr_tci, sizeof(tci_t));

        if (MC_DRV_OK != mc_ret)
        {
           /* mc_free_wsm(secwidevine_devid, (uint8_t *) secwidevine_tci);
            secwidevine_tci = NULL;*/
            mc_free_wsm(secwidevine_devid, (uint8_t *) secwidevinedr_tci);
            secwidevinedr_tci = NULL;
            mc_close_device(secwidevine_devid);
            MSG(ERR, "2.mc_open_session failed: %d\n", mc_ret);
            break;
        }
        secwidevine_session_ref = 1;

    } while(0);

    MSG(INFO, "secwidevine_session_open: ret=%d, ref=%d\n", mc_ret, secwidevine_session_ref);
    MSG(INFO, "driver sessionId = %d, deviceId = %d\n",
                secwidevinedr_session.session_id, secwidevinedr_session.device_id);

    mutex_unlock(&secwidevine_lock);

    if (MC_DRV_OK != mc_ret)
    {
        MSG(ERR, "secwidevine_session_open fail");
        return -ENXIO;
    }

    return 0;
}

//Close trustlet and driver
static int secwidevine_session_close(void)
{
    enum mc_result mc_ret = MC_DRV_OK;

    mutex_lock(&secwidevine_lock);

    do {
        /* session is already closed ? */
        if (secwidevine_session_ref == 0)
        {
            MSG(WRN, "secwidevine_session already closed\n");
            break;
        }

        if (secwidevine_session_ref > 1)
        {
            secwidevine_session_ref--;
            break;
        }

       /*  close session
        mc_ret = mc_close_session(&secwidevine_session);
        if (MC_DRV_OK != mc_ret)
        {
            MSG(ERR, "mc_close_session failed: %d\n", mc_ret);
            break;
        }

         free WSM for DCI
        mc_ret = mc_free_wsm(secwidevine_devid, (uint8_t*) secwidevine_tci);
        if (MC_DRV_OK != mc_ret)
        {
            MSG(ERR, "mc_free_wsm failed: %d\n", mc_ret);
            break;
        }
        secwidevine_tci = NULL;*/

        /* close session */
        mc_ret = mc_close_session(&secwidevinedr_session);
        if (MC_DRV_OK != mc_ret)
        {
            MSG(ERR, "2.mc_close_session failed: %d\n", mc_ret);
            break;
        }

        /* free WSM for DCI */
        mc_ret = mc_free_wsm(secwidevine_devid, (uint8_t*) secwidevinedr_tci);
        if (MC_DRV_OK != mc_ret)
        {
            MSG(ERR, "2.mc_free_wsm failed: %d\n", mc_ret);
            break;
        }
        secwidevinedr_tci = NULL;

        secwidevine_session_ref = 0;

        /* close device */
        mc_ret = mc_close_device(secwidevine_devid);
        if (MC_DRV_OK != mc_ret)
        {
            MSG(ERR, "mc_close_device failed: %d\n", mc_ret);
        }    

    } while(0);

    MSG(INFO, "secwidevine_session_close: ret=%d, ref=%d\n", mc_ret, secwidevine_session_ref);

    mutex_unlock(&secwidevine_lock);

    if (MC_DRV_OK != mc_ret)
    {
        return -ENXIO;
    }

    return 0;

}

static int secwidevine_open(struct inode *inode, struct file *file)
{
    /* open session */
    if (secwidevine_session_open() < 0)
    {
        MSG(ERR, "secwidevine_open fail - secwidevine_session_open fail");
        return -ENXIO;
    }
    return 0;
}

static int secwidevine_release(struct inode *inode, struct file *file)
{
    int ret = 0;

    ret = secwidevine_session_close();
    return ret;
}

static long secwidevine_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    int err = 0;

    struct secwidevine_param param;
    u32 handle;

    if (_IOC_TYPE(cmd) != SECWIDEVINE_IOC_MAGIC)
    {
        MSG(ERR,"Bad magic\n");
        return -ENOTTY;
    }
    if (_IOC_NR(cmd) > SECWIDEVINE_IOC_MAXNR)
    {
        MSG(ERR,"Bad ioc number\n");
        return -ENOTTY;
    }

    if (_IOC_DIR(cmd) & _IOC_READ)
    {
        err = !access_ok(VERIFY_WRITE, (void __user *)arg, _IOC_SIZE(cmd));
    }
    if (_IOC_DIR(cmd) & _IOC_WRITE)
    {
        err = !access_ok(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd));
    }

    if (err)
    {
        MSG(ERR, "verify read/write fail\n");
        return -EFAULT;
    }

    err = copy_from_user(&param, (void*)arg, sizeof(param));

    if (err)
    {
        MSG(ERR, "copy_from_user fail\n");
        return -EFAULT;
    }

    switch (cmd) {
    /*case SECWIDEVINE_GET_SESSION:
        if (!(file->f_mode & FMODE_WRITE))
        {
            MSG(ERR, "verify FMODE_WRITE fail\n");
            return -EROFS;
        }
        err = secwidevine_execute(CMD_SEC_WIDEVINE_GET_SESSION, &param);
        break;*/
    default:
        return -ENOTTY;
    }

    if (!err)
    {
        err = copy_to_user((void*)arg, &param, sizeof(param));
    }
    MSG(INFO, "copy_to_user result = %d\n", err);
    MSG(INFO, "sessionId = %d, deviceId = %d\n",
            param.session_handle.session_id, param.session_handle.device_id);
    return err;
}

static struct file_operations secwidevine_fops = {
    .owner   = THIS_MODULE,
    .open    = secwidevine_open,
    .release = secwidevine_release,
    .unlocked_ioctl = secwidevine_ioctl,
    .write   = NULL,
    .read    = NULL,
};

static int __init secwidevine_init(void)
{
#if 0
    struct proc_dir_entry *secwidevine_proc;
    secwidevine_proc = create_proc_entry("secwidevine0",
        (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH), NULL);
    
    if (IS_ERR(secwidevine_proc))
    {
        goto error;
    }

    secwidevine_proc->proc_fops = &secwidevine_fops;
#else
    proc_create("secwidevine0", (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH), NULL, &secwidevine_fops);
#endif

    return 0;

#if 0
error:
    return -1;
#endif
}

late_initcall(secwidevine_init);