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