]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * dvb_ca.c: generic DVB functions for EN50221 CAM interfaces | |
3 | * | |
4 | * Copyright (C) 2004 Andrew de Quincey | |
5 | * | |
6 | * Parts of this file were based on sources as follows: | |
7 | * | |
8 | * Copyright (C) 2003 Ralph Metzler <rjkm@metzlerbros.de> | |
9 | * | |
10 | * based on code: | |
11 | * | |
12 | * Copyright (C) 1999-2002 Ralph Metzler | |
13 | * & Marcus Metzler for convergence integrated media GmbH | |
14 | * | |
15 | * This program is free software; you can redistribute it and/or | |
16 | * modify it under the terms of the GNU General Public License | |
17 | * as published by the Free Software Foundation; either version 2 | |
18 | * of the License, or (at your option) any later version. | |
19 | * | |
20 | * This program is distributed in the hope that it will be useful, | |
21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
23 | * GNU General Public License for more details. | |
24 | * | |
25 | * You should have received a copy of the GNU General Public License | |
26 | * along with this program; if not, write to the Free Software | |
27 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
28 | * Or, point your browser to http://www.gnu.org/copyleft/gpl.html | |
29 | */ | |
30 | ||
31 | #include <linux/errno.h> | |
32 | #include <linux/slab.h> | |
33 | #include <linux/list.h> | |
34 | #include <linux/module.h> | |
1da177e4 LT |
35 | #include <linux/vmalloc.h> |
36 | #include <linux/delay.h> | |
ded92846 | 37 | #include <linux/spinlock.h> |
4e57b681 | 38 | #include <linux/sched.h> |
9320874a | 39 | #include <linux/kthread.h> |
1da177e4 LT |
40 | |
41 | #include "dvb_ca_en50221.h" | |
42 | #include "dvb_ringbuffer.h" | |
43 | ||
44 | static int dvb_ca_en50221_debug; | |
45 | ||
46 | module_param_named(cam_debug, dvb_ca_en50221_debug, int, 0644); | |
47 | MODULE_PARM_DESC(cam_debug, "enable verbose debug messages"); | |
48 | ||
49 | #define dprintk if (dvb_ca_en50221_debug) printk | |
50 | ||
50b447d5 | 51 | #define INIT_TIMEOUT_SECS 10 |
1da177e4 LT |
52 | |
53 | #define HOST_LINK_BUF_SIZE 0x200 | |
54 | ||
55 | #define RX_BUFFER_SIZE 65535 | |
56 | ||
57 | #define MAX_RX_PACKETS_PER_ITERATION 10 | |
58 | ||
59 | #define CTRLIF_DATA 0 | |
60 | #define CTRLIF_COMMAND 1 | |
61 | #define CTRLIF_STATUS 1 | |
62 | #define CTRLIF_SIZE_LOW 2 | |
63 | #define CTRLIF_SIZE_HIGH 3 | |
64 | ||
65 | #define CMDREG_HC 1 /* Host control */ | |
66 | #define CMDREG_SW 2 /* Size write */ | |
67 | #define CMDREG_SR 4 /* Size read */ | |
68 | #define CMDREG_RS 8 /* Reset interface */ | |
69 | #define CMDREG_FRIE 0x40 /* Enable FR interrupt */ | |
70 | #define CMDREG_DAIE 0x80 /* Enable DA interrupt */ | |
71 | #define IRQEN (CMDREG_DAIE) | |
72 | ||
73 | #define STATUSREG_RE 1 /* read error */ | |
74 | #define STATUSREG_WE 2 /* write error */ | |
75 | #define STATUSREG_FR 0x40 /* module free */ | |
76 | #define STATUSREG_DA 0x80 /* data available */ | |
77 | #define STATUSREG_TXERR (STATUSREG_RE|STATUSREG_WE) /* general transfer error */ | |
78 | ||
79 | ||
80 | #define DVB_CA_SLOTSTATE_NONE 0 | |
81 | #define DVB_CA_SLOTSTATE_UNINITIALISED 1 | |
82 | #define DVB_CA_SLOTSTATE_RUNNING 2 | |
83 | #define DVB_CA_SLOTSTATE_INVALID 3 | |
84 | #define DVB_CA_SLOTSTATE_WAITREADY 4 | |
85 | #define DVB_CA_SLOTSTATE_VALIDATE 5 | |
86 | #define DVB_CA_SLOTSTATE_WAITFR 6 | |
87 | #define DVB_CA_SLOTSTATE_LINKINIT 7 | |
88 | ||
89 | ||
90 | /* Information on a CA slot */ | |
91 | struct dvb_ca_slot { | |
92 | ||
93 | /* current state of the CAM */ | |
94 | int slot_state; | |
95 | ||
96 | /* Number of CAMCHANGES that have occurred since last processing */ | |
97 | atomic_t camchange_count; | |
98 | ||
99 | /* Type of last CAMCHANGE */ | |
100 | int camchange_type; | |
101 | ||
102 | /* base address of CAM config */ | |
103 | u32 config_base; | |
104 | ||
105 | /* value to write into Config Control register */ | |
106 | u8 config_option; | |
107 | ||
108 | /* if 1, the CAM supports DA IRQs */ | |
109 | u8 da_irq_supported:1; | |
110 | ||
111 | /* size of the buffer to use when talking to the CAM */ | |
112 | int link_buf_size; | |
113 | ||
1da177e4 LT |
114 | /* buffer for incoming packets */ |
115 | struct dvb_ringbuffer rx_buffer; | |
116 | ||
117 | /* timer used during various states of the slot */ | |
118 | unsigned long timeout; | |
119 | }; | |
120 | ||
121 | /* Private CA-interface information */ | |
122 | struct dvb_ca_private { | |
123 | ||
124 | /* pointer back to the public data structure */ | |
125 | struct dvb_ca_en50221 *pub; | |
126 | ||
127 | /* the DVB device */ | |
128 | struct dvb_device *dvbdev; | |
129 | ||
130 | /* Flags describing the interface (DVB_CA_FLAG_*) */ | |
131 | u32 flags; | |
132 | ||
133 | /* number of slots supported by this CA interface */ | |
134 | unsigned int slot_count; | |
135 | ||
136 | /* information on each slot */ | |
137 | struct dvb_ca_slot *slot_info; | |
138 | ||
139 | /* wait queues for read() and write() operations */ | |
140 | wait_queue_head_t wait_queue; | |
141 | ||
142 | /* PID of the monitoring thread */ | |
9320874a | 143 | struct task_struct *thread; |
1da177e4 LT |
144 | |
145 | /* Flag indicating if the CA device is open */ | |
146 | unsigned int open:1; | |
147 | ||
148 | /* Flag indicating the thread should wake up now */ | |
149 | unsigned int wakeup:1; | |
150 | ||
151 | /* Delay the main thread should use */ | |
152 | unsigned long delay; | |
153 | ||
154 | /* Slot to start looking for data to read from in the next user-space read operation */ | |
155 | int next_read_slot; | |
156 | }; | |
157 | ||
158 | static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca); | |
159 | static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount); | |
160 | static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount); | |
161 | ||
162 | ||
163 | /** | |
164 | * Safely find needle in haystack. | |
165 | * | |
166 | * @param haystack Buffer to look in. | |
167 | * @param hlen Number of bytes in haystack. | |
168 | * @param needle Buffer to find. | |
169 | * @param nlen Number of bytes in needle. | |
170 | * @return Pointer into haystack needle was found at, or NULL if not found. | |
171 | */ | |
260f8d7c | 172 | static char *findstr(char * haystack, int hlen, char * needle, int nlen) |
1da177e4 LT |
173 | { |
174 | int i; | |
175 | ||
176 | if (hlen < nlen) | |
177 | return NULL; | |
178 | ||
179 | for (i = 0; i <= hlen - nlen; i++) { | |
180 | if (!strncmp(haystack + i, needle, nlen)) | |
181 | return haystack + i; | |
182 | } | |
183 | ||
184 | return NULL; | |
185 | } | |
186 | ||
187 | ||
188 | ||
189 | /* ******************************************************************************** */ | |
190 | /* EN50221 physical interface functions */ | |
191 | ||
192 | ||
193 | /** | |
194 | * Check CAM status. | |
195 | */ | |
196 | static int dvb_ca_en50221_check_camstatus(struct dvb_ca_private *ca, int slot) | |
197 | { | |
198 | int slot_status; | |
199 | int cam_present_now; | |
200 | int cam_changed; | |
201 | ||
202 | /* IRQ mode */ | |
203 | if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE) { | |
204 | return (atomic_read(&ca->slot_info[slot].camchange_count) != 0); | |
205 | } | |
206 | ||
207 | /* poll mode */ | |
208 | slot_status = ca->pub->poll_slot_status(ca->pub, slot, ca->open); | |
209 | ||
210 | cam_present_now = (slot_status & DVB_CA_EN50221_POLL_CAM_PRESENT) ? 1 : 0; | |
211 | cam_changed = (slot_status & DVB_CA_EN50221_POLL_CAM_CHANGED) ? 1 : 0; | |
212 | if (!cam_changed) { | |
213 | int cam_present_old = (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE); | |
214 | cam_changed = (cam_present_now != cam_present_old); | |
215 | } | |
216 | ||
217 | if (cam_changed) { | |
218 | if (!cam_present_now) { | |
219 | ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED; | |
220 | } else { | |
221 | ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_INSERTED; | |
222 | } | |
223 | atomic_set(&ca->slot_info[slot].camchange_count, 1); | |
224 | } else { | |
225 | if ((ca->slot_info[slot].slot_state == DVB_CA_SLOTSTATE_WAITREADY) && | |
226 | (slot_status & DVB_CA_EN50221_POLL_CAM_READY)) { | |
227 | // move to validate state if reset is completed | |
228 | ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_VALIDATE; | |
229 | } | |
230 | } | |
231 | ||
232 | return cam_changed; | |
233 | } | |
234 | ||
235 | ||
236 | /** | |
237 | * Wait for flags to become set on the STATUS register on a CAM interface, | |
238 | * checking for errors and timeout. | |
239 | * | |
240 | * @param ca CA instance. | |
241 | * @param slot Slot on interface. | |
242 | * @param waitfor Flags to wait for. | |
243 | * @param timeout_ms Timeout in milliseconds. | |
244 | * | |
245 | * @return 0 on success, nonzero on error. | |
246 | */ | |
247 | static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private *ca, int slot, | |
248 | u8 waitfor, int timeout_hz) | |
249 | { | |
250 | unsigned long timeout; | |
251 | unsigned long start; | |
252 | ||
253 | dprintk("%s\n", __FUNCTION__); | |
254 | ||
255 | /* loop until timeout elapsed */ | |
256 | start = jiffies; | |
257 | timeout = jiffies + timeout_hz; | |
258 | while (1) { | |
259 | /* read the status and check for error */ | |
260 | int res = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS); | |
261 | if (res < 0) | |
262 | return -EIO; | |
263 | ||
264 | /* if we got the flags, it was successful! */ | |
265 | if (res & waitfor) { | |
266 | dprintk("%s succeeded timeout:%lu\n", __FUNCTION__, jiffies - start); | |
267 | return 0; | |
268 | } | |
269 | ||
270 | /* check for timeout */ | |
271 | if (time_after(jiffies, timeout)) { | |
272 | break; | |
273 | } | |
274 | ||
275 | /* wait for a bit */ | |
276 | msleep(1); | |
277 | } | |
278 | ||
279 | dprintk("%s failed timeout:%lu\n", __FUNCTION__, jiffies - start); | |
280 | ||
281 | /* if we get here, we've timed out */ | |
282 | return -ETIMEDOUT; | |
283 | } | |
284 | ||
285 | ||
286 | /** | |
287 | * Initialise the link layer connection to a CAM. | |
288 | * | |
289 | * @param ca CA instance. | |
290 | * @param slot Slot id. | |
291 | * | |
292 | * @return 0 on success, nonzero on failure. | |
293 | */ | |
294 | static int dvb_ca_en50221_link_init(struct dvb_ca_private *ca, int slot) | |
295 | { | |
296 | int ret; | |
297 | int buf_size; | |
298 | u8 buf[2]; | |
299 | ||
300 | dprintk("%s\n", __FUNCTION__); | |
301 | ||
302 | /* we'll be determining these during this function */ | |
303 | ca->slot_info[slot].da_irq_supported = 0; | |
304 | ||
305 | /* set the host link buffer size temporarily. it will be overwritten with the | |
306 | * real negotiated size later. */ | |
307 | ca->slot_info[slot].link_buf_size = 2; | |
308 | ||
309 | /* read the buffer size from the CAM */ | |
310 | if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN | CMDREG_SR)) != 0) | |
311 | return ret; | |
312 | if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_DA, HZ / 10)) != 0) | |
313 | return ret; | |
314 | if ((ret = dvb_ca_en50221_read_data(ca, slot, buf, 2)) != 2) | |
315 | return -EIO; | |
316 | if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0) | |
317 | return ret; | |
318 | ||
319 | /* store it, and choose the minimum of our buffer and the CAM's buffer size */ | |
320 | buf_size = (buf[0] << 8) | buf[1]; | |
321 | if (buf_size > HOST_LINK_BUF_SIZE) | |
322 | buf_size = HOST_LINK_BUF_SIZE; | |
323 | ca->slot_info[slot].link_buf_size = buf_size; | |
324 | buf[0] = buf_size >> 8; | |
325 | buf[1] = buf_size & 0xff; | |
326 | dprintk("Chosen link buffer size of %i\n", buf_size); | |
327 | ||
328 | /* write the buffer size to the CAM */ | |
329 | if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN | CMDREG_SW)) != 0) | |
330 | return ret; | |
331 | if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_FR, HZ / 10)) != 0) | |
332 | return ret; | |
333 | if ((ret = dvb_ca_en50221_write_data(ca, slot, buf, 2)) != 2) | |
334 | return -EIO; | |
335 | if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0) | |
336 | return ret; | |
337 | ||
338 | /* success */ | |
339 | return 0; | |
340 | } | |
341 | ||
342 | /** | |
343 | * Read a tuple from attribute memory. | |
344 | * | |
345 | * @param ca CA instance. | |
346 | * @param slot Slot id. | |
347 | * @param address Address to read from. Updated. | |
348 | * @param tupleType Tuple id byte. Updated. | |
349 | * @param tupleLength Tuple length. Updated. | |
350 | * @param tuple Dest buffer for tuple (must be 256 bytes). Updated. | |
351 | * | |
352 | * @return 0 on success, nonzero on error. | |
353 | */ | |
354 | static int dvb_ca_en50221_read_tuple(struct dvb_ca_private *ca, int slot, | |
355 | int *address, int *tupleType, int *tupleLength, u8 * tuple) | |
356 | { | |
357 | int i; | |
358 | int _tupleType; | |
359 | int _tupleLength; | |
360 | int _address = *address; | |
361 | ||
362 | /* grab the next tuple length and type */ | |
363 | if ((_tupleType = ca->pub->read_attribute_mem(ca->pub, slot, _address)) < 0) | |
364 | return _tupleType; | |
365 | if (_tupleType == 0xff) { | |
366 | dprintk("END OF CHAIN TUPLE type:0x%x\n", _tupleType); | |
367 | *address += 2; | |
368 | *tupleType = _tupleType; | |
369 | *tupleLength = 0; | |
370 | return 0; | |
371 | } | |
372 | if ((_tupleLength = ca->pub->read_attribute_mem(ca->pub, slot, _address + 2)) < 0) | |
373 | return _tupleLength; | |
374 | _address += 4; | |
375 | ||
376 | dprintk("TUPLE type:0x%x length:%i\n", _tupleType, _tupleLength); | |
377 | ||
378 | /* read in the whole tuple */ | |
379 | for (i = 0; i < _tupleLength; i++) { | |
380 | tuple[i] = ca->pub->read_attribute_mem(ca->pub, slot, _address + (i * 2)); | |
381 | dprintk(" 0x%02x: 0x%02x %c\n", | |
382 | i, tuple[i] & 0xff, | |
383 | ((tuple[i] > 31) && (tuple[i] < 127)) ? tuple[i] : '.'); | |
384 | } | |
385 | _address += (_tupleLength * 2); | |
386 | ||
387 | // success | |
388 | *tupleType = _tupleType; | |
389 | *tupleLength = _tupleLength; | |
390 | *address = _address; | |
391 | return 0; | |
392 | } | |
393 | ||
394 | ||
395 | /** | |
396 | * Parse attribute memory of a CAM module, extracting Config register, and checking | |
397 | * it is a DVB CAM module. | |
398 | * | |
399 | * @param ca CA instance. | |
400 | * @param slot Slot id. | |
401 | * | |
402 | * @return 0 on success, <0 on failure. | |
403 | */ | |
404 | static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private *ca, int slot) | |
405 | { | |
406 | int address = 0; | |
407 | int tupleLength; | |
408 | int tupleType; | |
409 | u8 tuple[257]; | |
410 | char *dvb_str; | |
411 | int rasz; | |
412 | int status; | |
413 | int got_cftableentry = 0; | |
414 | int end_chain = 0; | |
415 | int i; | |
416 | u16 manfid = 0; | |
417 | u16 devid = 0; | |
418 | ||
419 | ||
420 | // CISTPL_DEVICE_0A | |
421 | if ((status = | |
422 | dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0) | |
423 | return status; | |
424 | if (tupleType != 0x1D) | |
425 | return -EINVAL; | |
426 | ||
427 | ||
428 | ||
429 | // CISTPL_DEVICE_0C | |
430 | if ((status = | |
431 | dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0) | |
432 | return status; | |
433 | if (tupleType != 0x1C) | |
434 | return -EINVAL; | |
435 | ||
436 | ||
437 | ||
438 | // CISTPL_VERS_1 | |
439 | if ((status = | |
440 | dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0) | |
441 | return status; | |
442 | if (tupleType != 0x15) | |
443 | return -EINVAL; | |
444 | ||
445 | ||
446 | ||
447 | // CISTPL_MANFID | |
448 | if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, | |
449 | &tupleLength, tuple)) < 0) | |
450 | return status; | |
451 | if (tupleType != 0x20) | |
452 | return -EINVAL; | |
453 | if (tupleLength != 4) | |
454 | return -EINVAL; | |
455 | manfid = (tuple[1] << 8) | tuple[0]; | |
456 | devid = (tuple[3] << 8) | tuple[2]; | |
457 | ||
458 | ||
459 | ||
460 | // CISTPL_CONFIG | |
461 | if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, | |
462 | &tupleLength, tuple)) < 0) | |
463 | return status; | |
464 | if (tupleType != 0x1A) | |
465 | return -EINVAL; | |
466 | if (tupleLength < 3) | |
467 | return -EINVAL; | |
468 | ||
469 | /* extract the configbase */ | |
470 | rasz = tuple[0] & 3; | |
471 | if (tupleLength < (3 + rasz + 14)) | |
472 | return -EINVAL; | |
473 | ca->slot_info[slot].config_base = 0; | |
474 | for (i = 0; i < rasz + 1; i++) { | |
475 | ca->slot_info[slot].config_base |= (tuple[2 + i] << (8 * i)); | |
476 | } | |
477 | ||
478 | /* check it contains the correct DVB string */ | |
260f8d7c | 479 | dvb_str = findstr((char *)tuple, tupleLength, "DVB_CI_V", 8); |
1da177e4 LT |
480 | if (dvb_str == NULL) |
481 | return -EINVAL; | |
482 | if (tupleLength < ((dvb_str - (char *) tuple) + 12)) | |
483 | return -EINVAL; | |
484 | ||
485 | /* is it a version we support? */ | |
486 | if (strncmp(dvb_str + 8, "1.00", 4)) { | |
487 | printk("dvb_ca adapter %d: Unsupported DVB CAM module version %c%c%c%c\n", | |
488 | ca->dvbdev->adapter->num, dvb_str[8], dvb_str[9], dvb_str[10], dvb_str[11]); | |
489 | return -EINVAL; | |
490 | } | |
491 | ||
492 | /* process the CFTABLE_ENTRY tuples, and any after those */ | |
493 | while ((!end_chain) && (address < 0x1000)) { | |
494 | if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, | |
afd1a0c9 | 495 | &tupleLength, tuple)) < 0) |
1da177e4 LT |
496 | return status; |
497 | switch (tupleType) { | |
498 | case 0x1B: // CISTPL_CFTABLE_ENTRY | |
499 | if (tupleLength < (2 + 11 + 17)) | |
500 | break; | |
501 | ||
502 | /* if we've already parsed one, just use it */ | |
503 | if (got_cftableentry) | |
504 | break; | |
505 | ||
506 | /* get the config option */ | |
507 | ca->slot_info[slot].config_option = tuple[0] & 0x3f; | |
508 | ||
509 | /* OK, check it contains the correct strings */ | |
260f8d7c OE |
510 | if ((findstr((char *)tuple, tupleLength, "DVB_HOST", 8) == NULL) || |
511 | (findstr((char *)tuple, tupleLength, "DVB_CI_MODULE", 13) == NULL)) | |
1da177e4 LT |
512 | break; |
513 | ||
514 | got_cftableentry = 1; | |
515 | break; | |
516 | ||
517 | case 0x14: // CISTPL_NO_LINK | |
518 | break; | |
519 | ||
520 | case 0xFF: // CISTPL_END | |
521 | end_chain = 1; | |
522 | break; | |
523 | ||
524 | default: /* Unknown tuple type - just skip this tuple and move to the next one */ | |
525 | dprintk("dvb_ca: Skipping unknown tuple type:0x%x length:0x%x\n", tupleType, | |
526 | tupleLength); | |
527 | break; | |
528 | } | |
529 | } | |
530 | ||
531 | if ((address > 0x1000) || (!got_cftableentry)) | |
532 | return -EINVAL; | |
533 | ||
534 | dprintk("Valid DVB CAM detected MANID:%x DEVID:%x CONFIGBASE:0x%x CONFIGOPTION:0x%x\n", | |
535 | manfid, devid, ca->slot_info[slot].config_base, ca->slot_info[slot].config_option); | |
536 | ||
537 | // success! | |
538 | return 0; | |
539 | } | |
540 | ||
541 | ||
542 | /** | |
543 | * Set CAM's configoption correctly. | |
544 | * | |
545 | * @param ca CA instance. | |
546 | * @param slot Slot containing the CAM. | |
547 | */ | |
548 | static int dvb_ca_en50221_set_configoption(struct dvb_ca_private *ca, int slot) | |
549 | { | |
550 | int configoption; | |
551 | ||
552 | dprintk("%s\n", __FUNCTION__); | |
553 | ||
554 | /* set the config option */ | |
555 | ca->pub->write_attribute_mem(ca->pub, slot, | |
556 | ca->slot_info[slot].config_base, | |
557 | ca->slot_info[slot].config_option); | |
558 | ||
559 | /* check it */ | |
560 | configoption = ca->pub->read_attribute_mem(ca->pub, slot, ca->slot_info[slot].config_base); | |
561 | dprintk("Set configoption 0x%x, read configoption 0x%x\n", | |
562 | ca->slot_info[slot].config_option, configoption & 0x3f); | |
563 | ||
564 | /* fine! */ | |
565 | return 0; | |
566 | ||
567 | } | |
568 | ||
569 | ||
570 | /** | |
571 | * This function talks to an EN50221 CAM control interface. It reads a buffer of | |
572 | * data from the CAM. The data can either be stored in a supplied buffer, or | |
573 | * automatically be added to the slot's rx_buffer. | |
574 | * | |
575 | * @param ca CA instance. | |
576 | * @param slot Slot to read from. | |
577 | * @param ebuf If non-NULL, the data will be written to this buffer. If NULL, | |
578 | * the data will be added into the buffering system as a normal fragment. | |
579 | * @param ecount Size of ebuf. Ignored if ebuf is NULL. | |
580 | * | |
581 | * @return Number of bytes read, or < 0 on error | |
582 | */ | |
583 | static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount) | |
584 | { | |
585 | int bytes_read; | |
586 | int status; | |
587 | u8 buf[HOST_LINK_BUF_SIZE]; | |
588 | int i; | |
589 | ||
590 | dprintk("%s\n", __FUNCTION__); | |
591 | ||
592 | /* check if we have space for a link buf in the rx_buffer */ | |
593 | if (ebuf == NULL) { | |
594 | int buf_free; | |
595 | ||
1da177e4 | 596 | if (ca->slot_info[slot].rx_buffer.data == NULL) { |
1da177e4 LT |
597 | status = -EIO; |
598 | goto exit; | |
599 | } | |
600 | buf_free = dvb_ringbuffer_free(&ca->slot_info[slot].rx_buffer); | |
1da177e4 LT |
601 | |
602 | if (buf_free < (ca->slot_info[slot].link_buf_size + DVB_RINGBUFFER_PKTHDRSIZE)) { | |
603 | status = -EAGAIN; | |
604 | goto exit; | |
605 | } | |
606 | } | |
607 | ||
608 | /* check if there is data available */ | |
609 | if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0) | |
610 | goto exit; | |
611 | if (!(status & STATUSREG_DA)) { | |
612 | /* no data */ | |
613 | status = 0; | |
614 | goto exit; | |
615 | } | |
616 | ||
617 | /* read the amount of data */ | |
618 | if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH)) < 0) | |
619 | goto exit; | |
620 | bytes_read = status << 8; | |
621 | if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW)) < 0) | |
622 | goto exit; | |
623 | bytes_read |= status; | |
624 | ||
625 | /* check it will fit */ | |
626 | if (ebuf == NULL) { | |
627 | if (bytes_read > ca->slot_info[slot].link_buf_size) { | |
628 | printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the link buffer size (%i > %i)!\n", | |
629 | ca->dvbdev->adapter->num, bytes_read, ca->slot_info[slot].link_buf_size); | |
630 | ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT; | |
631 | status = -EIO; | |
632 | goto exit; | |
633 | } | |
634 | if (bytes_read < 2) { | |
635 | printk("dvb_ca adapter %d: CAM sent a buffer that was less than 2 bytes!\n", | |
636 | ca->dvbdev->adapter->num); | |
637 | ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT; | |
638 | status = -EIO; | |
639 | goto exit; | |
640 | } | |
641 | } else { | |
642 | if (bytes_read > ecount) { | |
643 | printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the ecount size!\n", | |
644 | ca->dvbdev->adapter->num); | |
645 | status = -EIO; | |
646 | goto exit; | |
647 | } | |
648 | } | |
649 | ||
650 | /* fill the buffer */ | |
651 | for (i = 0; i < bytes_read; i++) { | |
652 | /* read byte and check */ | |
653 | if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_DATA)) < 0) | |
654 | goto exit; | |
655 | ||
656 | /* OK, store it in the buffer */ | |
657 | buf[i] = status; | |
658 | } | |
659 | ||
660 | /* check for read error (RE should now be 0) */ | |
661 | if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0) | |
662 | goto exit; | |
663 | if (status & STATUSREG_RE) { | |
664 | ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT; | |
665 | status = -EIO; | |
666 | goto exit; | |
667 | } | |
668 | ||
669 | /* OK, add it to the receive buffer, or copy into external buffer if supplied */ | |
670 | if (ebuf == NULL) { | |
1da177e4 | 671 | if (ca->slot_info[slot].rx_buffer.data == NULL) { |
1da177e4 LT |
672 | status = -EIO; |
673 | goto exit; | |
674 | } | |
675 | dvb_ringbuffer_pkt_write(&ca->slot_info[slot].rx_buffer, buf, bytes_read); | |
1da177e4 LT |
676 | } else { |
677 | memcpy(ebuf, buf, bytes_read); | |
678 | } | |
679 | ||
680 | dprintk("Received CA packet for slot %i connection id 0x%x last_frag:%i size:0x%x\n", slot, | |
681 | buf[0], (buf[1] & 0x80) == 0, bytes_read); | |
682 | ||
683 | /* wake up readers when a last_fragment is received */ | |
684 | if ((buf[1] & 0x80) == 0x00) { | |
685 | wake_up_interruptible(&ca->wait_queue); | |
686 | } | |
687 | status = bytes_read; | |
688 | ||
689 | exit: | |
690 | return status; | |
691 | } | |
692 | ||
693 | ||
694 | /** | |
695 | * This function talks to an EN50221 CAM control interface. It writes a buffer of data | |
696 | * to a CAM. | |
697 | * | |
698 | * @param ca CA instance. | |
699 | * @param slot Slot to write to. | |
700 | * @param ebuf The data in this buffer is treated as a complete link-level packet to | |
701 | * be written. | |
702 | * @param count Size of ebuf. | |
703 | * | |
704 | * @return Number of bytes written, or < 0 on error. | |
705 | */ | |
706 | static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot, u8 * buf, int bytes_write) | |
707 | { | |
708 | int status; | |
709 | int i; | |
710 | ||
711 | dprintk("%s\n", __FUNCTION__); | |
712 | ||
713 | ||
714 | // sanity check | |
715 | if (bytes_write > ca->slot_info[slot].link_buf_size) | |
716 | return -EINVAL; | |
717 | ||
718 | /* check if interface is actually waiting for us to read from it, or if a read is in progress */ | |
719 | if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0) | |
720 | goto exitnowrite; | |
721 | if (status & (STATUSREG_DA | STATUSREG_RE)) { | |
722 | status = -EAGAIN; | |
723 | goto exitnowrite; | |
724 | } | |
725 | ||
726 | /* OK, set HC bit */ | |
727 | if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, | |
728 | IRQEN | CMDREG_HC)) != 0) | |
729 | goto exit; | |
730 | ||
731 | /* check if interface is still free */ | |
732 | if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0) | |
733 | goto exit; | |
734 | if (!(status & STATUSREG_FR)) { | |
735 | /* it wasn't free => try again later */ | |
736 | status = -EAGAIN; | |
737 | goto exit; | |
738 | } | |
739 | ||
740 | /* send the amount of data */ | |
741 | if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH, bytes_write >> 8)) != 0) | |
742 | goto exit; | |
743 | if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW, | |
744 | bytes_write & 0xff)) != 0) | |
745 | goto exit; | |
746 | ||
747 | /* send the buffer */ | |
748 | for (i = 0; i < bytes_write; i++) { | |
749 | if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_DATA, buf[i])) != 0) | |
750 | goto exit; | |
751 | } | |
752 | ||
753 | /* check for write error (WE should now be 0) */ | |
754 | if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0) | |
755 | goto exit; | |
756 | if (status & STATUSREG_WE) { | |
757 | ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT; | |
758 | status = -EIO; | |
759 | goto exit; | |
760 | } | |
761 | status = bytes_write; | |
762 | ||
763 | dprintk("Wrote CA packet for slot %i, connection id 0x%x last_frag:%i size:0x%x\n", slot, | |
764 | buf[0], (buf[1] & 0x80) == 0, bytes_write); | |
765 | ||
766 | exit: | |
767 | ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN); | |
768 | ||
769 | exitnowrite: | |
770 | return status; | |
771 | } | |
772 | EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq); | |
773 | ||
774 | ||
775 | ||
776 | /* ******************************************************************************** */ | |
777 | /* EN50221 higher level functions */ | |
778 | ||
779 | ||
780 | /** | |
781 | * A CAM has been removed => shut it down. | |
782 | * | |
783 | * @param ca CA instance. | |
784 | * @param slot Slot to shut down. | |
785 | */ | |
786 | static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private *ca, int slot) | |
787 | { | |
788 | dprintk("%s\n", __FUNCTION__); | |
789 | ||
1da177e4 LT |
790 | ca->pub->slot_shutdown(ca->pub, slot); |
791 | ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE; | |
1da177e4 LT |
792 | |
793 | /* need to wake up all processes to check if they're now | |
794 | trying to write to a defunct CAM */ | |
795 | wake_up_interruptible(&ca->wait_queue); | |
796 | ||
797 | dprintk("Slot %i shutdown\n", slot); | |
798 | ||
799 | /* success */ | |
800 | return 0; | |
801 | } | |
802 | EXPORT_SYMBOL(dvb_ca_en50221_camready_irq); | |
803 | ||
804 | ||
805 | /** | |
806 | * A CAMCHANGE IRQ has occurred. | |
807 | * | |
808 | * @param ca CA instance. | |
809 | * @param slot Slot concerned. | |
810 | * @param change_type One of the DVB_CA_CAMCHANGE_* values. | |
811 | */ | |
812 | void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221 *pubca, int slot, int change_type) | |
813 | { | |
0c53c70f | 814 | struct dvb_ca_private *ca = pubca->private; |
1da177e4 LT |
815 | |
816 | dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot, change_type); | |
817 | ||
818 | switch (change_type) { | |
819 | case DVB_CA_EN50221_CAMCHANGE_REMOVED: | |
820 | case DVB_CA_EN50221_CAMCHANGE_INSERTED: | |
821 | break; | |
822 | ||
823 | default: | |
824 | return; | |
825 | } | |
826 | ||
827 | ca->slot_info[slot].camchange_type = change_type; | |
828 | atomic_inc(&ca->slot_info[slot].camchange_count); | |
829 | dvb_ca_en50221_thread_wakeup(ca); | |
830 | } | |
831 | EXPORT_SYMBOL(dvb_ca_en50221_frda_irq); | |
832 | ||
833 | ||
834 | /** | |
835 | * A CAMREADY IRQ has occurred. | |
836 | * | |
837 | * @param ca CA instance. | |
838 | * @param slot Slot concerned. | |
839 | */ | |
840 | void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221 *pubca, int slot) | |
841 | { | |
0c53c70f | 842 | struct dvb_ca_private *ca = pubca->private; |
1da177e4 LT |
843 | |
844 | dprintk("CAMREADY IRQ slot:%i\n", slot); | |
845 | ||
846 | if (ca->slot_info[slot].slot_state == DVB_CA_SLOTSTATE_WAITREADY) { | |
847 | ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_VALIDATE; | |
848 | dvb_ca_en50221_thread_wakeup(ca); | |
849 | } | |
850 | } | |
851 | ||
852 | ||
853 | /** | |
854 | * An FR or DA IRQ has occurred. | |
855 | * | |
856 | * @param ca CA instance. | |
857 | * @param slot Slot concerned. | |
858 | */ | |
859 | void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221 *pubca, int slot) | |
860 | { | |
0c53c70f | 861 | struct dvb_ca_private *ca = pubca->private; |
1da177e4 LT |
862 | int flags; |
863 | ||
864 | dprintk("FR/DA IRQ slot:%i\n", slot); | |
865 | ||
866 | switch (ca->slot_info[slot].slot_state) { | |
867 | case DVB_CA_SLOTSTATE_LINKINIT: | |
868 | flags = ca->pub->read_cam_control(pubca, slot, CTRLIF_STATUS); | |
869 | if (flags & STATUSREG_DA) { | |
870 | dprintk("CAM supports DA IRQ\n"); | |
871 | ca->slot_info[slot].da_irq_supported = 1; | |
872 | } | |
873 | break; | |
874 | ||
875 | case DVB_CA_SLOTSTATE_RUNNING: | |
876 | if (ca->open) | |
ded92846 | 877 | dvb_ca_en50221_thread_wakeup(ca); |
1da177e4 LT |
878 | break; |
879 | } | |
880 | } | |
881 | ||
882 | ||
883 | ||
884 | /* ******************************************************************************** */ | |
885 | /* EN50221 thread functions */ | |
886 | ||
887 | /** | |
888 | * Wake up the DVB CA thread | |
889 | * | |
890 | * @param ca CA instance. | |
891 | */ | |
892 | static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca) | |
893 | { | |
894 | ||
895 | dprintk("%s\n", __FUNCTION__); | |
896 | ||
897 | ca->wakeup = 1; | |
898 | mb(); | |
9320874a | 899 | wake_up_process(ca->thread); |
1da177e4 LT |
900 | } |
901 | ||
1da177e4 LT |
902 | /** |
903 | * Update the delay used by the thread. | |
904 | * | |
905 | * @param ca CA instance. | |
906 | */ | |
907 | static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private *ca) | |
908 | { | |
909 | int delay; | |
910 | int curdelay = 100000000; | |
911 | int slot; | |
912 | ||
913 | for (slot = 0; slot < ca->slot_count; slot++) { | |
914 | switch (ca->slot_info[slot].slot_state) { | |
915 | default: | |
916 | case DVB_CA_SLOTSTATE_NONE: | |
917 | case DVB_CA_SLOTSTATE_INVALID: | |
918 | delay = HZ * 60; | |
919 | if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) { | |
920 | delay = HZ / 10; | |
921 | } | |
922 | break; | |
923 | ||
924 | case DVB_CA_SLOTSTATE_UNINITIALISED: | |
925 | case DVB_CA_SLOTSTATE_WAITREADY: | |
926 | case DVB_CA_SLOTSTATE_VALIDATE: | |
927 | case DVB_CA_SLOTSTATE_WAITFR: | |
928 | case DVB_CA_SLOTSTATE_LINKINIT: | |
929 | delay = HZ / 10; | |
930 | break; | |
931 | ||
932 | case DVB_CA_SLOTSTATE_RUNNING: | |
933 | delay = HZ * 60; | |
934 | if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) { | |
935 | delay = HZ / 10; | |
936 | } | |
937 | if (ca->open) { | |
938 | if ((!ca->slot_info[slot].da_irq_supported) || | |
939 | (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_DA))) { | |
940 | delay = HZ / 10; | |
941 | } | |
942 | } | |
943 | break; | |
944 | } | |
945 | ||
946 | if (delay < curdelay) | |
947 | curdelay = delay; | |
948 | } | |
949 | ||
950 | ca->delay = curdelay; | |
951 | } | |
952 | ||
953 | ||
954 | ||
955 | /** | |
956 | * Kernel thread which monitors CA slots for CAM changes, and performs data transfers. | |
957 | */ | |
958 | static int dvb_ca_en50221_thread(void *data) | |
959 | { | |
0c53c70f | 960 | struct dvb_ca_private *ca = data; |
1da177e4 LT |
961 | int slot; |
962 | int flags; | |
963 | int status; | |
964 | int pktcount; | |
965 | void *rxbuf; | |
966 | ||
967 | dprintk("%s\n", __FUNCTION__); | |
968 | ||
1da177e4 LT |
969 | /* choose the correct initial delay */ |
970 | dvb_ca_en50221_thread_update_delay(ca); | |
971 | ||
972 | /* main loop */ | |
9320874a | 973 | while (!kthread_should_stop()) { |
1da177e4 | 974 | /* sleep for a bit */ |
9320874a CH |
975 | while (!ca->wakeup) { |
976 | set_current_state(TASK_INTERRUPTIBLE); | |
977 | schedule_timeout(ca->delay); | |
978 | if (kthread_should_stop()) | |
979 | return 0; | |
1da177e4 LT |
980 | } |
981 | ca->wakeup = 0; | |
982 | ||
983 | /* go through all the slots processing them */ | |
984 | for (slot = 0; slot < ca->slot_count; slot++) { | |
985 | ||
986 | // check the cam status + deal with CAMCHANGEs | |
987 | while (dvb_ca_en50221_check_camstatus(ca, slot)) { | |
988 | /* clear down an old CI slot if necessary */ | |
989 | if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE) | |
990 | dvb_ca_en50221_slot_shutdown(ca, slot); | |
991 | ||
992 | /* if a CAM is NOW present, initialise it */ | |
993 | if (ca->slot_info[slot].camchange_type == DVB_CA_EN50221_CAMCHANGE_INSERTED) { | |
994 | ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_UNINITIALISED; | |
995 | } | |
996 | ||
997 | /* we've handled one CAMCHANGE */ | |
998 | dvb_ca_en50221_thread_update_delay(ca); | |
999 | atomic_dec(&ca->slot_info[slot].camchange_count); | |
1000 | } | |
1001 | ||
1002 | // CAM state machine | |
1003 | switch (ca->slot_info[slot].slot_state) { | |
1004 | case DVB_CA_SLOTSTATE_NONE: | |
1005 | case DVB_CA_SLOTSTATE_INVALID: | |
1006 | // no action needed | |
1007 | break; | |
1008 | ||
1009 | case DVB_CA_SLOTSTATE_UNINITIALISED: | |
1010 | ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITREADY; | |
1011 | ca->pub->slot_reset(ca->pub, slot); | |
1012 | ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ); | |
1013 | break; | |
1014 | ||
1015 | case DVB_CA_SLOTSTATE_WAITREADY: | |
1016 | if (time_after(jiffies, ca->slot_info[slot].timeout)) { | |
1017 | printk("dvb_ca adaptor %d: PC card did not respond :(\n", | |
1018 | ca->dvbdev->adapter->num); | |
1019 | ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID; | |
1020 | dvb_ca_en50221_thread_update_delay(ca); | |
1021 | break; | |
1022 | } | |
1023 | // no other action needed; will automatically change state when ready | |
1024 | break; | |
1025 | ||
1026 | case DVB_CA_SLOTSTATE_VALIDATE: | |
5c1208ba AQ |
1027 | if (dvb_ca_en50221_parse_attributes(ca, slot) != 0) { |
1028 | /* we need this extra check for annoying interfaces like the budget-av */ | |
1029 | if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) && | |
1030 | (ca->pub->poll_slot_status)) { | |
1031 | int status = ca->pub->poll_slot_status(ca->pub, slot, 0); | |
1032 | if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) { | |
1033 | ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE; | |
1034 | dvb_ca_en50221_thread_update_delay(ca); | |
1035 | break; | |
1036 | } | |
1037 | } | |
1038 | ||
1da177e4 LT |
1039 | printk("dvb_ca adapter %d: Invalid PC card inserted :(\n", |
1040 | ca->dvbdev->adapter->num); | |
1041 | ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID; | |
1042 | dvb_ca_en50221_thread_update_delay(ca); | |
1043 | break; | |
1044 | } | |
1045 | if (dvb_ca_en50221_set_configoption(ca, slot) != 0) { | |
1046 | printk("dvb_ca adapter %d: Unable to initialise CAM :(\n", | |
1047 | ca->dvbdev->adapter->num); | |
1048 | ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID; | |
1049 | dvb_ca_en50221_thread_update_delay(ca); | |
1050 | break; | |
1051 | } | |
1052 | if (ca->pub->write_cam_control(ca->pub, slot, | |
1053 | CTRLIF_COMMAND, CMDREG_RS) != 0) { | |
1054 | printk("dvb_ca adapter %d: Unable to reset CAM IF\n", | |
1055 | ca->dvbdev->adapter->num); | |
1056 | ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID; | |
1057 | dvb_ca_en50221_thread_update_delay(ca); | |
1058 | break; | |
1059 | } | |
1060 | dprintk("DVB CAM validated successfully\n"); | |
1061 | ||
1062 | ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ); | |
1063 | ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITFR; | |
1064 | ca->wakeup = 1; | |
1065 | break; | |
1066 | ||
1067 | case DVB_CA_SLOTSTATE_WAITFR: | |
1068 | if (time_after(jiffies, ca->slot_info[slot].timeout)) { | |
1069 | printk("dvb_ca adapter %d: DVB CAM did not respond :(\n", | |
1070 | ca->dvbdev->adapter->num); | |
1071 | ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID; | |
1072 | dvb_ca_en50221_thread_update_delay(ca); | |
1073 | break; | |
1074 | } | |
1075 | ||
1076 | flags = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS); | |
1077 | if (flags & STATUSREG_FR) { | |
1078 | ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT; | |
1079 | ca->wakeup = 1; | |
1080 | } | |
1081 | break; | |
1082 | ||
1083 | case DVB_CA_SLOTSTATE_LINKINIT: | |
1084 | if (dvb_ca_en50221_link_init(ca, slot) != 0) { | |
5c1208ba AQ |
1085 | /* we need this extra check for annoying interfaces like the budget-av */ |
1086 | if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) && | |
1087 | (ca->pub->poll_slot_status)) { | |
1088 | int status = ca->pub->poll_slot_status(ca->pub, slot, 0); | |
1089 | if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) { | |
1090 | ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE; | |
1091 | dvb_ca_en50221_thread_update_delay(ca); | |
1092 | break; | |
1093 | } | |
1094 | } | |
1095 | ||
1da177e4 LT |
1096 | printk("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n", ca->dvbdev->adapter->num); |
1097 | ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID; | |
1098 | dvb_ca_en50221_thread_update_delay(ca); | |
1099 | break; | |
1100 | } | |
1101 | ||
ded92846 AQ |
1102 | if (ca->slot_info[slot].rx_buffer.data == NULL) { |
1103 | rxbuf = vmalloc(RX_BUFFER_SIZE); | |
1104 | if (rxbuf == NULL) { | |
1105 | printk("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n", ca->dvbdev->adapter->num); | |
1106 | ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID; | |
1107 | dvb_ca_en50221_thread_update_delay(ca); | |
1108 | break; | |
1109 | } | |
1110 | dvb_ringbuffer_init(&ca->slot_info[slot].rx_buffer, rxbuf, RX_BUFFER_SIZE); | |
1da177e4 | 1111 | } |
1da177e4 LT |
1112 | |
1113 | ca->pub->slot_ts_enable(ca->pub, slot); | |
1114 | ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_RUNNING; | |
1115 | dvb_ca_en50221_thread_update_delay(ca); | |
1116 | printk("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n", ca->dvbdev->adapter->num); | |
1117 | break; | |
1118 | ||
1119 | case DVB_CA_SLOTSTATE_RUNNING: | |
1120 | if (!ca->open) | |
1121 | continue; | |
1122 | ||
ded92846 | 1123 | // poll slots for data |
1da177e4 LT |
1124 | pktcount = 0; |
1125 | while ((status = dvb_ca_en50221_read_data(ca, slot, NULL, 0)) > 0) { | |
1126 | if (!ca->open) | |
1127 | break; | |
1128 | ||
1129 | /* if a CAMCHANGE occurred at some point, do not do any more processing of this slot */ | |
1130 | if (dvb_ca_en50221_check_camstatus(ca, slot)) { | |
1131 | // we dont want to sleep on the next iteration so we can handle the cam change | |
1132 | ca->wakeup = 1; | |
1133 | break; | |
1134 | } | |
1135 | ||
1136 | /* check if we've hit our limit this time */ | |
1137 | if (++pktcount >= MAX_RX_PACKETS_PER_ITERATION) { | |
1138 | // dont sleep; there is likely to be more data to read | |
1139 | ca->wakeup = 1; | |
1140 | break; | |
1141 | } | |
1142 | } | |
1143 | break; | |
1144 | } | |
1145 | } | |
1146 | } | |
1147 | ||
1da177e4 LT |
1148 | return 0; |
1149 | } | |
1150 | ||
1151 | ||
1152 | ||
1153 | /* ******************************************************************************** */ | |
1154 | /* EN50221 IO interface functions */ | |
1155 | ||
1156 | /** | |
1157 | * Real ioctl implementation. | |
1158 | * NOTE: CA_SEND_MSG/CA_GET_MSG ioctls have userspace buffers passed to them. | |
1159 | * | |
1160 | * @param inode Inode concerned. | |
1161 | * @param file File concerned. | |
1162 | * @param cmd IOCTL command. | |
1163 | * @param arg Associated argument. | |
1164 | * | |
1165 | * @return 0 on success, <0 on error. | |
1166 | */ | |
1167 | static int dvb_ca_en50221_io_do_ioctl(struct inode *inode, struct file *file, | |
1168 | unsigned int cmd, void *parg) | |
1169 | { | |
0c53c70f JS |
1170 | struct dvb_device *dvbdev = file->private_data; |
1171 | struct dvb_ca_private *ca = dvbdev->priv; | |
1da177e4 LT |
1172 | int err = 0; |
1173 | int slot; | |
1174 | ||
1175 | dprintk("%s\n", __FUNCTION__); | |
1176 | ||
1177 | switch (cmd) { | |
1178 | case CA_RESET: | |
1179 | for (slot = 0; slot < ca->slot_count; slot++) { | |
1180 | if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE) { | |
1181 | dvb_ca_en50221_slot_shutdown(ca, slot); | |
1182 | if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE) | |
1183 | dvb_ca_en50221_camchange_irq(ca->pub, | |
1184 | slot, | |
1185 | DVB_CA_EN50221_CAMCHANGE_INSERTED); | |
1186 | } | |
1187 | } | |
1188 | ca->next_read_slot = 0; | |
1189 | dvb_ca_en50221_thread_wakeup(ca); | |
1190 | break; | |
1191 | ||
1192 | case CA_GET_CAP: { | |
0c53c70f | 1193 | struct ca_caps *caps = parg; |
1da177e4 LT |
1194 | |
1195 | caps->slot_num = ca->slot_count; | |
1196 | caps->slot_type = CA_CI_LINK; | |
1197 | caps->descr_num = 0; | |
1198 | caps->descr_type = 0; | |
1199 | break; | |
1200 | } | |
1201 | ||
1202 | case CA_GET_SLOT_INFO: { | |
0c53c70f | 1203 | struct ca_slot_info *info = parg; |
1da177e4 LT |
1204 | |
1205 | if ((info->num > ca->slot_count) || (info->num < 0)) | |
1206 | return -EINVAL; | |
1207 | ||
1208 | info->type = CA_CI_LINK; | |
1209 | info->flags = 0; | |
1210 | if ((ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_NONE) | |
1211 | && (ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_INVALID)) { | |
1212 | info->flags = CA_CI_MODULE_PRESENT; | |
1213 | } | |
1214 | if (ca->slot_info[info->num].slot_state == DVB_CA_SLOTSTATE_RUNNING) { | |
1215 | info->flags |= CA_CI_MODULE_READY; | |
1216 | } | |
1217 | break; | |
1218 | } | |
1219 | ||
1220 | default: | |
1221 | err = -EINVAL; | |
1222 | break; | |
1223 | } | |
1224 | ||
1225 | return err; | |
1226 | } | |
1227 | ||
1228 | ||
1229 | /** | |
1230 | * Wrapper for ioctl implementation. | |
1231 | * | |
1232 | * @param inode Inode concerned. | |
1233 | * @param file File concerned. | |
1234 | * @param cmd IOCTL command. | |
1235 | * @param arg Associated argument. | |
1236 | * | |
1237 | * @return 0 on success, <0 on error. | |
1238 | */ | |
1239 | static int dvb_ca_en50221_io_ioctl(struct inode *inode, struct file *file, | |
1240 | unsigned int cmd, unsigned long arg) | |
1241 | { | |
1242 | return dvb_usercopy(inode, file, cmd, arg, dvb_ca_en50221_io_do_ioctl); | |
1243 | } | |
1244 | ||
1245 | ||
1246 | /** | |
1247 | * Implementation of write() syscall. | |
1248 | * | |
1249 | * @param file File structure. | |
1250 | * @param buf Source buffer. | |
1251 | * @param count Size of source buffer. | |
1252 | * @param ppos Position in file (ignored). | |
1253 | * | |
1254 | * @return Number of bytes read, or <0 on error. | |
1255 | */ | |
1256 | static ssize_t dvb_ca_en50221_io_write(struct file *file, | |
1257 | const char __user * buf, size_t count, loff_t * ppos) | |
1258 | { | |
0c53c70f JS |
1259 | struct dvb_device *dvbdev = file->private_data; |
1260 | struct dvb_ca_private *ca = dvbdev->priv; | |
1da177e4 LT |
1261 | u8 slot, connection_id; |
1262 | int status; | |
260f8d7c | 1263 | u8 fragbuf[HOST_LINK_BUF_SIZE]; |
1da177e4 LT |
1264 | int fragpos = 0; |
1265 | int fraglen; | |
1266 | unsigned long timeout; | |
1267 | int written; | |
1268 | ||
1269 | dprintk("%s\n", __FUNCTION__); | |
1270 | ||
1271 | /* Incoming packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */ | |
1272 | if (count < 2) | |
1273 | return -EINVAL; | |
1274 | ||
1275 | /* extract slot & connection id */ | |
1276 | if (copy_from_user(&slot, buf, 1)) | |
1277 | return -EFAULT; | |
1278 | if (copy_from_user(&connection_id, buf + 1, 1)) | |
1279 | return -EFAULT; | |
1280 | buf += 2; | |
1281 | count -= 2; | |
1282 | ||
1283 | /* check if the slot is actually running */ | |
1284 | if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING) | |
1285 | return -EINVAL; | |
1286 | ||
1287 | /* fragment the packets & store in the buffer */ | |
1288 | while (fragpos < count) { | |
1289 | fraglen = ca->slot_info[slot].link_buf_size - 2; | |
1290 | if ((count - fragpos) < fraglen) | |
1291 | fraglen = count - fragpos; | |
1292 | ||
1293 | fragbuf[0] = connection_id; | |
1294 | fragbuf[1] = ((fragpos + fraglen) < count) ? 0x80 : 0x00; | |
1295 | if ((status = copy_from_user(fragbuf + 2, buf + fragpos, fraglen)) != 0) | |
1296 | goto exit; | |
1297 | ||
1298 | timeout = jiffies + HZ / 2; | |
1299 | written = 0; | |
1300 | while (!time_after(jiffies, timeout)) { | |
1301 | /* check the CAM hasn't been removed/reset in the meantime */ | |
1302 | if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING) { | |
1303 | status = -EIO; | |
1304 | goto exit; | |
1305 | } | |
1306 | ||
1307 | status = dvb_ca_en50221_write_data(ca, slot, fragbuf, fraglen + 2); | |
1308 | if (status == (fraglen + 2)) { | |
1309 | written = 1; | |
1310 | break; | |
1311 | } | |
1312 | if (status != -EAGAIN) | |
1313 | goto exit; | |
1314 | ||
1315 | msleep(1); | |
1316 | } | |
1317 | if (!written) { | |
1318 | status = -EIO; | |
1319 | goto exit; | |
1320 | } | |
1321 | ||
1322 | fragpos += fraglen; | |
1323 | } | |
1324 | status = count + 2; | |
1325 | ||
1326 | exit: | |
1327 | return status; | |
1328 | } | |
1329 | ||
1330 | ||
1331 | /** | |
1332 | * Condition for waking up in dvb_ca_en50221_io_read_condition | |
1333 | */ | |
ded92846 AQ |
1334 | static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private *ca, |
1335 | int *result, int *_slot) | |
1da177e4 LT |
1336 | { |
1337 | int slot; | |
1338 | int slot_count = 0; | |
1339 | int idx; | |
ded92846 | 1340 | size_t fraglen; |
1da177e4 LT |
1341 | int connection_id = -1; |
1342 | int found = 0; | |
1343 | u8 hdr[2]; | |
1344 | ||
1345 | slot = ca->next_read_slot; | |
1346 | while ((slot_count < ca->slot_count) && (!found)) { | |
1347 | if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING) | |
1348 | goto nextslot; | |
1349 | ||
1da177e4 | 1350 | if (ca->slot_info[slot].rx_buffer.data == NULL) { |
1da177e4 LT |
1351 | return 0; |
1352 | } | |
1353 | ||
1354 | idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen); | |
1355 | while (idx != -1) { | |
1356 | dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2, 0); | |
1357 | if (connection_id == -1) | |
1358 | connection_id = hdr[0]; | |
1359 | if ((hdr[0] == connection_id) && ((hdr[1] & 0x80) == 0)) { | |
1360 | *_slot = slot; | |
1361 | found = 1; | |
1362 | break; | |
1363 | } | |
1364 | ||
1365 | idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen); | |
1366 | } | |
1367 | ||
ded92846 | 1368 | nextslot: |
1da177e4 LT |
1369 | slot = (slot + 1) % ca->slot_count; |
1370 | slot_count++; | |
1371 | } | |
1372 | ||
1373 | ca->next_read_slot = slot; | |
1374 | return found; | |
1375 | } | |
1376 | ||
1377 | ||
1378 | /** | |
1379 | * Implementation of read() syscall. | |
1380 | * | |
1381 | * @param file File structure. | |
1382 | * @param buf Destination buffer. | |
1383 | * @param count Size of destination buffer. | |
1384 | * @param ppos Position in file (ignored). | |
1385 | * | |
1386 | * @return Number of bytes read, or <0 on error. | |
1387 | */ | |
1388 | static ssize_t dvb_ca_en50221_io_read(struct file *file, char __user * buf, | |
1389 | size_t count, loff_t * ppos) | |
1390 | { | |
0c53c70f JS |
1391 | struct dvb_device *dvbdev = file->private_data; |
1392 | struct dvb_ca_private *ca = dvbdev->priv; | |
1da177e4 LT |
1393 | int status; |
1394 | int result = 0; | |
1395 | u8 hdr[2]; | |
1396 | int slot; | |
1397 | int connection_id = -1; | |
1398 | size_t idx, idx2; | |
1399 | int last_fragment = 0; | |
1400 | size_t fraglen; | |
1401 | int pktlen; | |
1402 | int dispose = 0; | |
1403 | ||
1404 | dprintk("%s\n", __FUNCTION__); | |
1405 | ||
1406 | /* Outgoing packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */ | |
1407 | if (count < 2) | |
1408 | return -EINVAL; | |
1409 | ||
1410 | /* wait for some data */ | |
1411 | if ((status = dvb_ca_en50221_io_read_condition(ca, &result, &slot)) == 0) { | |
1412 | ||
1413 | /* if we're in nonblocking mode, exit immediately */ | |
1414 | if (file->f_flags & O_NONBLOCK) | |
1415 | return -EWOULDBLOCK; | |
1416 | ||
1417 | /* wait for some data */ | |
1418 | status = wait_event_interruptible(ca->wait_queue, | |
1419 | dvb_ca_en50221_io_read_condition | |
1420 | (ca, &result, &slot)); | |
1421 | } | |
1422 | if ((status < 0) || (result < 0)) { | |
1423 | if (result) | |
1424 | return result; | |
1425 | return status; | |
1426 | } | |
1427 | ||
1428 | idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen); | |
1429 | pktlen = 2; | |
1430 | do { | |
1431 | if (idx == -1) { | |
1432 | printk("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n", ca->dvbdev->adapter->num); | |
1433 | status = -EIO; | |
1434 | goto exit; | |
1435 | } | |
1436 | ||
1437 | dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2, 0); | |
1438 | if (connection_id == -1) | |
1439 | connection_id = hdr[0]; | |
1440 | if (hdr[0] == connection_id) { | |
1441 | if (pktlen < count) { | |
1442 | if ((pktlen + fraglen - 2) > count) { | |
1443 | fraglen = count - pktlen; | |
1444 | } else { | |
1445 | fraglen -= 2; | |
1446 | } | |
1447 | ||
1448 | if ((status = dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 2, | |
260f8d7c | 1449 | (u8 *)buf + pktlen, fraglen, 1)) < 0) { |
1da177e4 LT |
1450 | goto exit; |
1451 | } | |
1452 | pktlen += fraglen; | |
1453 | } | |
1454 | ||
1455 | if ((hdr[1] & 0x80) == 0) | |
1456 | last_fragment = 1; | |
1457 | dispose = 1; | |
1458 | } | |
1459 | ||
1460 | idx2 = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen); | |
1461 | if (dispose) | |
1462 | dvb_ringbuffer_pkt_dispose(&ca->slot_info[slot].rx_buffer, idx); | |
1463 | idx = idx2; | |
1464 | dispose = 0; | |
1465 | } while (!last_fragment); | |
1466 | ||
1467 | hdr[0] = slot; | |
1468 | hdr[1] = connection_id; | |
1469 | if ((status = copy_to_user(buf, hdr, 2)) != 0) | |
1470 | goto exit; | |
1471 | status = pktlen; | |
1472 | ||
ded92846 | 1473 | exit: |
1da177e4 LT |
1474 | return status; |
1475 | } | |
1476 | ||
1477 | ||
1478 | /** | |
1479 | * Implementation of file open syscall. | |
1480 | * | |
1481 | * @param inode Inode concerned. | |
1482 | * @param file File concerned. | |
1483 | * | |
1484 | * @return 0 on success, <0 on failure. | |
1485 | */ | |
1486 | static int dvb_ca_en50221_io_open(struct inode *inode, struct file *file) | |
1487 | { | |
0c53c70f JS |
1488 | struct dvb_device *dvbdev = file->private_data; |
1489 | struct dvb_ca_private *ca = dvbdev->priv; | |
1da177e4 LT |
1490 | int err; |
1491 | int i; | |
1492 | ||
1493 | dprintk("%s\n", __FUNCTION__); | |
1494 | ||
1495 | if (!try_module_get(ca->pub->owner)) | |
1496 | return -EIO; | |
1497 | ||
1498 | err = dvb_generic_open(inode, file); | |
226835d7 MS |
1499 | if (err < 0) { |
1500 | module_put(ca->pub->owner); | |
1da177e4 | 1501 | return err; |
226835d7 | 1502 | } |
1da177e4 LT |
1503 | |
1504 | for (i = 0; i < ca->slot_count; i++) { | |
1505 | ||
1506 | if (ca->slot_info[i].slot_state == DVB_CA_SLOTSTATE_RUNNING) { | |
1da177e4 | 1507 | if (ca->slot_info[i].rx_buffer.data != NULL) { |
ded92846 AQ |
1508 | /* it is safe to call this here without locks because |
1509 | * ca->open == 0. Data is not read in this case */ | |
1da177e4 LT |
1510 | dvb_ringbuffer_flush(&ca->slot_info[i].rx_buffer); |
1511 | } | |
1da177e4 LT |
1512 | } |
1513 | } | |
1514 | ||
1515 | ca->open = 1; | |
1516 | dvb_ca_en50221_thread_update_delay(ca); | |
1517 | dvb_ca_en50221_thread_wakeup(ca); | |
1518 | ||
1519 | return 0; | |
1520 | } | |
1521 | ||
1522 | ||
1523 | /** | |
1524 | * Implementation of file close syscall. | |
1525 | * | |
1526 | * @param inode Inode concerned. | |
1527 | * @param file File concerned. | |
1528 | * | |
1529 | * @return 0 on success, <0 on failure. | |
1530 | */ | |
1531 | static int dvb_ca_en50221_io_release(struct inode *inode, struct file *file) | |
1532 | { | |
0c53c70f JS |
1533 | struct dvb_device *dvbdev = file->private_data; |
1534 | struct dvb_ca_private *ca = dvbdev->priv; | |
0c12c1bf | 1535 | int err; |
1da177e4 LT |
1536 | |
1537 | dprintk("%s\n", __FUNCTION__); | |
1538 | ||
1539 | /* mark the CA device as closed */ | |
1540 | ca->open = 0; | |
1541 | dvb_ca_en50221_thread_update_delay(ca); | |
1542 | ||
1543 | err = dvb_generic_release(inode, file); | |
1544 | ||
1545 | module_put(ca->pub->owner); | |
1546 | ||
0c12c1bf | 1547 | return err; |
1da177e4 LT |
1548 | } |
1549 | ||
1550 | ||
1551 | /** | |
1552 | * Implementation of poll() syscall. | |
1553 | * | |
1554 | * @param file File concerned. | |
1555 | * @param wait poll wait table. | |
1556 | * | |
1557 | * @return Standard poll mask. | |
1558 | */ | |
1559 | static unsigned int dvb_ca_en50221_io_poll(struct file *file, poll_table * wait) | |
1560 | { | |
0c53c70f JS |
1561 | struct dvb_device *dvbdev = file->private_data; |
1562 | struct dvb_ca_private *ca = dvbdev->priv; | |
1da177e4 LT |
1563 | unsigned int mask = 0; |
1564 | int slot; | |
1565 | int result = 0; | |
1566 | ||
1567 | dprintk("%s\n", __FUNCTION__); | |
1568 | ||
1569 | if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) { | |
1da177e4 LT |
1570 | mask |= POLLIN; |
1571 | } | |
1572 | ||
1573 | /* if there is something, return now */ | |
1574 | if (mask) | |
1575 | return mask; | |
1576 | ||
1577 | /* wait for something to happen */ | |
1578 | poll_wait(file, &ca->wait_queue, wait); | |
1579 | ||
1580 | if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) { | |
1da177e4 LT |
1581 | mask |= POLLIN; |
1582 | } | |
1583 | ||
1584 | return mask; | |
1585 | } | |
1586 | EXPORT_SYMBOL(dvb_ca_en50221_init); | |
1587 | ||
1588 | ||
1589 | static struct file_operations dvb_ca_fops = { | |
1590 | .owner = THIS_MODULE, | |
1591 | .read = dvb_ca_en50221_io_read, | |
1592 | .write = dvb_ca_en50221_io_write, | |
1593 | .ioctl = dvb_ca_en50221_io_ioctl, | |
1594 | .open = dvb_ca_en50221_io_open, | |
1595 | .release = dvb_ca_en50221_io_release, | |
1596 | .poll = dvb_ca_en50221_io_poll, | |
1597 | }; | |
1598 | ||
1599 | static struct dvb_device dvbdev_ca = { | |
1600 | .priv = NULL, | |
1601 | .users = 1, | |
1602 | .readers = 1, | |
1603 | .writers = 1, | |
1604 | .fops = &dvb_ca_fops, | |
1605 | }; | |
1606 | ||
1607 | ||
1608 | /* ******************************************************************************** */ | |
1609 | /* Initialisation/shutdown functions */ | |
1610 | ||
1611 | ||
1612 | /** | |
1613 | * Initialise a new DVB CA EN50221 interface device. | |
1614 | * | |
1615 | * @param dvb_adapter DVB adapter to attach the new CA device to. | |
1616 | * @param ca The dvb_ca instance. | |
1617 | * @param flags Flags describing the CA device (DVB_CA_FLAG_*). | |
1618 | * @param slot_count Number of slots supported. | |
1619 | * | |
1620 | * @return 0 on success, nonzero on failure | |
1621 | */ | |
1622 | int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter, | |
1623 | struct dvb_ca_en50221 *pubca, int flags, int slot_count) | |
1624 | { | |
1625 | int ret; | |
1626 | struct dvb_ca_private *ca = NULL; | |
1627 | int i; | |
1628 | ||
1629 | dprintk("%s\n", __FUNCTION__); | |
1630 | ||
1631 | if (slot_count < 1) | |
1632 | return -EINVAL; | |
1633 | ||
1634 | /* initialise the system data */ | |
7408187d | 1635 | if ((ca = kzalloc(sizeof(struct dvb_ca_private), GFP_KERNEL)) == NULL) { |
1da177e4 LT |
1636 | ret = -ENOMEM; |
1637 | goto error; | |
1638 | } | |
1da177e4 LT |
1639 | ca->pub = pubca; |
1640 | ca->flags = flags; | |
1641 | ca->slot_count = slot_count; | |
7408187d | 1642 | if ((ca->slot_info = kcalloc(slot_count, sizeof(struct dvb_ca_slot), GFP_KERNEL)) == NULL) { |
1da177e4 LT |
1643 | ret = -ENOMEM; |
1644 | goto error; | |
1645 | } | |
1da177e4 | 1646 | init_waitqueue_head(&ca->wait_queue); |
1da177e4 LT |
1647 | ca->open = 0; |
1648 | ca->wakeup = 0; | |
1649 | ca->next_read_slot = 0; | |
1650 | pubca->private = ca; | |
1651 | ||
1652 | /* register the DVB device */ | |
1653 | ret = dvb_register_device(dvb_adapter, &ca->dvbdev, &dvbdev_ca, ca, DVB_DEVICE_CA); | |
1654 | if (ret) | |
1655 | goto error; | |
1656 | ||
1657 | /* now initialise each slot */ | |
1658 | for (i = 0; i < slot_count; i++) { | |
1659 | memset(&ca->slot_info[i], 0, sizeof(struct dvb_ca_slot)); | |
1660 | ca->slot_info[i].slot_state = DVB_CA_SLOTSTATE_NONE; | |
1661 | atomic_set(&ca->slot_info[i].camchange_count, 0); | |
1662 | ca->slot_info[i].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED; | |
1da177e4 LT |
1663 | } |
1664 | ||
1665 | if (signal_pending(current)) { | |
1666 | ret = -EINTR; | |
1667 | goto error; | |
1668 | } | |
1669 | mb(); | |
1670 | ||
1671 | /* create a kthread for monitoring this CA device */ | |
9320874a CH |
1672 | ca->thread = kthread_run(dvb_ca_en50221_thread, ca, "kdvb-ca-%i:%i", |
1673 | ca->dvbdev->adapter->num, ca->dvbdev->id); | |
1674 | if (IS_ERR(ca->thread)) { | |
1675 | ret = PTR_ERR(ca->thread); | |
1676 | printk("dvb_ca_init: failed to start kernel_thread (%d)\n", | |
1677 | ret); | |
1da177e4 LT |
1678 | goto error; |
1679 | } | |
1da177e4 LT |
1680 | return 0; |
1681 | ||
ded92846 | 1682 | error: |
1da177e4 LT |
1683 | if (ca != NULL) { |
1684 | if (ca->dvbdev != NULL) | |
1685 | dvb_unregister_device(ca->dvbdev); | |
1686 | kfree(ca->slot_info); | |
1687 | kfree(ca); | |
1688 | } | |
1689 | pubca->private = NULL; | |
1690 | return ret; | |
1691 | } | |
1692 | EXPORT_SYMBOL(dvb_ca_en50221_release); | |
1693 | ||
1694 | ||
1695 | ||
1696 | /** | |
1697 | * Release a DVB CA EN50221 interface device. | |
1698 | * | |
1699 | * @param ca_dev The dvb_device_t instance for the CA device. | |
1700 | * @param ca The associated dvb_ca instance. | |
1701 | */ | |
1702 | void dvb_ca_en50221_release(struct dvb_ca_en50221 *pubca) | |
1703 | { | |
0c53c70f | 1704 | struct dvb_ca_private *ca = pubca->private; |
1da177e4 LT |
1705 | int i; |
1706 | ||
1707 | dprintk("%s\n", __FUNCTION__); | |
1708 | ||
1709 | /* shutdown the thread if there was one */ | |
9320874a | 1710 | kthread_stop(ca->thread); |
1da177e4 LT |
1711 | |
1712 | for (i = 0; i < ca->slot_count; i++) { | |
1713 | dvb_ca_en50221_slot_shutdown(ca, i); | |
35dc0fef | 1714 | vfree(ca->slot_info[i].rx_buffer.data); |
1da177e4 LT |
1715 | } |
1716 | kfree(ca->slot_info); | |
1717 | dvb_unregister_device(ca->dvbdev); | |
1718 | kfree(ca); | |
1719 | pubca->private = NULL; | |
1720 | } |