]>
Commit | Line | Data |
---|---|---|
1a59d1b8 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
b5e47729 SR |
2 | /* |
3 | * nosy - Snoop mode driver for TI PCILynx 1394 controllers | |
4 | * Copyright (C) 2002-2007 Kristian Høgsberg | |
28646821 SR |
5 | */ |
6 | ||
7429b17d | 7 | #include <linux/device.h> |
28646821 | 8 | #include <linux/errno.h> |
b5e47729 | 9 | #include <linux/fs.h> |
28646821 | 10 | #include <linux/init.h> |
b5e47729 SR |
11 | #include <linux/interrupt.h> |
12 | #include <linux/io.h> | |
13 | #include <linux/kernel.h> | |
424d66ce | 14 | #include <linux/kref.h> |
b5e47729 SR |
15 | #include <linux/miscdevice.h> |
16 | #include <linux/module.h> | |
424d66ce | 17 | #include <linux/mutex.h> |
28646821 | 18 | #include <linux/pci.h> |
28646821 | 19 | #include <linux/poll.h> |
b5e47729 SR |
20 | #include <linux/sched.h> /* required for linux/wait.h */ |
21 | #include <linux/slab.h> | |
22 | #include <linux/spinlock.h> | |
2ae4b6b2 | 23 | #include <linux/time64.h> |
b5e47729 SR |
24 | #include <linux/timex.h> |
25 | #include <linux/uaccess.h> | |
26 | #include <linux/wait.h> | |
e894d1d7 | 27 | #include <linux/dma-mapping.h> |
60063497 | 28 | #include <linux/atomic.h> |
b5e47729 | 29 | #include <asm/byteorder.h> |
28646821 SR |
30 | |
31 | #include "nosy.h" | |
32 | #include "nosy-user.h" | |
33 | ||
34 | #define TCODE_PHY_PACKET 0x10 | |
35 | #define PCI_DEVICE_ID_TI_PCILYNX 0x8000 | |
36 | ||
b5e47729 | 37 | static char driver_name[] = KBUILD_MODNAME; |
28646821 | 38 | |
28646821 SR |
39 | /* this is the physical layout of a PCL, its size is 128 bytes */ |
40 | struct pcl { | |
fd8c8d46 SR |
41 | __le32 next; |
42 | __le32 async_error_next; | |
43 | u32 user_data; | |
44 | __le32 pcl_status; | |
45 | __le32 remaining_transfer_count; | |
46 | __le32 next_data_buffer; | |
47 | struct { | |
48 | __le32 control; | |
49 | __le32 pointer; | |
50 | } buffer[13]; | |
51 | }; | |
28646821 SR |
52 | |
53 | struct packet { | |
b5e47729 | 54 | unsigned int length; |
28646821 SR |
55 | char data[0]; |
56 | }; | |
57 | ||
58 | struct packet_buffer { | |
59 | char *data; | |
60 | size_t capacity; | |
61 | long total_packet_count, lost_packet_count; | |
62 | atomic_t size; | |
b5e47729 SR |
63 | struct packet *head, *tail; |
64 | wait_queue_head_t wait; | |
28646821 SR |
65 | }; |
66 | ||
67 | struct pcilynx { | |
68 | struct pci_dev *pci_device; | |
c89db7b8 | 69 | __iomem char *registers; |
28646821 SR |
70 | |
71 | struct pcl *rcv_start_pcl, *rcv_pcl; | |
fd8c8d46 | 72 | __le32 *rcv_buffer; |
28646821 SR |
73 | |
74 | dma_addr_t rcv_start_pcl_bus, rcv_pcl_bus, rcv_buffer_bus; | |
75 | ||
76 | spinlock_t client_list_lock; | |
77 | struct list_head client_list; | |
78 | ||
79 | struct miscdevice misc; | |
424d66ce SR |
80 | struct list_head link; |
81 | struct kref kref; | |
28646821 SR |
82 | }; |
83 | ||
424d66ce SR |
84 | static inline struct pcilynx * |
85 | lynx_get(struct pcilynx *lynx) | |
86 | { | |
87 | kref_get(&lynx->kref); | |
88 | ||
89 | return lynx; | |
90 | } | |
91 | ||
92 | static void | |
93 | lynx_release(struct kref *kref) | |
94 | { | |
95 | kfree(container_of(kref, struct pcilynx, kref)); | |
96 | } | |
97 | ||
98 | static inline void | |
99 | lynx_put(struct pcilynx *lynx) | |
100 | { | |
101 | kref_put(&lynx->kref, lynx_release); | |
102 | } | |
103 | ||
28646821 SR |
104 | struct client { |
105 | struct pcilynx *lynx; | |
c7b2a99c | 106 | u32 tcode_mask; |
28646821 SR |
107 | struct packet_buffer buffer; |
108 | struct list_head link; | |
109 | }; | |
110 | ||
424d66ce SR |
111 | static DEFINE_MUTEX(card_mutex); |
112 | static LIST_HEAD(card_list); | |
28646821 SR |
113 | |
114 | static int | |
115 | packet_buffer_init(struct packet_buffer *buffer, size_t capacity) | |
116 | { | |
117 | buffer->data = kmalloc(capacity, GFP_KERNEL); | |
118 | if (buffer->data == NULL) | |
119 | return -ENOMEM; | |
120 | buffer->head = (struct packet *) buffer->data; | |
121 | buffer->tail = (struct packet *) buffer->data; | |
122 | buffer->capacity = capacity; | |
123 | buffer->lost_packet_count = 0; | |
124 | atomic_set(&buffer->size, 0); | |
b5e47729 | 125 | init_waitqueue_head(&buffer->wait); |
28646821 SR |
126 | |
127 | return 0; | |
128 | } | |
129 | ||
130 | static void | |
131 | packet_buffer_destroy(struct packet_buffer *buffer) | |
132 | { | |
133 | kfree(buffer->data); | |
134 | } | |
135 | ||
136 | static int | |
c89db7b8 | 137 | packet_buffer_get(struct client *client, char __user *data, size_t user_length) |
28646821 | 138 | { |
424d66ce | 139 | struct packet_buffer *buffer = &client->buffer; |
28646821 SR |
140 | size_t length; |
141 | char *end; | |
142 | ||
143 | if (wait_event_interruptible(buffer->wait, | |
424d66ce SR |
144 | atomic_read(&buffer->size) > 0) || |
145 | list_empty(&client->lynx->link)) | |
28646821 SR |
146 | return -ERESTARTSYS; |
147 | ||
424d66ce SR |
148 | if (atomic_read(&buffer->size) == 0) |
149 | return -ENODEV; | |
150 | ||
28646821 SR |
151 | /* FIXME: Check length <= user_length. */ |
152 | ||
153 | end = buffer->data + buffer->capacity; | |
154 | length = buffer->head->length; | |
155 | ||
156 | if (&buffer->head->data[length] < end) { | |
157 | if (copy_to_user(data, buffer->head->data, length)) | |
158 | return -EFAULT; | |
159 | buffer->head = (struct packet *) &buffer->head->data[length]; | |
b5e47729 | 160 | } else { |
28646821 SR |
161 | size_t split = end - buffer->head->data; |
162 | ||
163 | if (copy_to_user(data, buffer->head->data, split)) | |
164 | return -EFAULT; | |
165 | if (copy_to_user(data + split, buffer->data, length - split)) | |
166 | return -EFAULT; | |
167 | buffer->head = (struct packet *) &buffer->data[length - split]; | |
168 | } | |
169 | ||
b5e47729 SR |
170 | /* |
171 | * Decrease buffer->size as the last thing, since this is what | |
28646821 | 172 | * keeps the interrupt from overwriting the packet we are |
b5e47729 SR |
173 | * retrieving from the buffer. |
174 | */ | |
175 | atomic_sub(sizeof(struct packet) + length, &buffer->size); | |
28646821 SR |
176 | |
177 | return length; | |
178 | } | |
179 | ||
180 | static void | |
181 | packet_buffer_put(struct packet_buffer *buffer, void *data, size_t length) | |
182 | { | |
183 | char *end; | |
184 | ||
185 | buffer->total_packet_count++; | |
186 | ||
b5e47729 SR |
187 | if (buffer->capacity < |
188 | atomic_read(&buffer->size) + sizeof(struct packet) + length) { | |
28646821 SR |
189 | buffer->lost_packet_count++; |
190 | return; | |
191 | } | |
192 | ||
193 | end = buffer->data + buffer->capacity; | |
194 | buffer->tail->length = length; | |
195 | ||
196 | if (&buffer->tail->data[length] < end) { | |
197 | memcpy(buffer->tail->data, data, length); | |
198 | buffer->tail = (struct packet *) &buffer->tail->data[length]; | |
b5e47729 | 199 | } else { |
28646821 SR |
200 | size_t split = end - buffer->tail->data; |
201 | ||
202 | memcpy(buffer->tail->data, data, split); | |
203 | memcpy(buffer->data, data + split, length - split); | |
204 | buffer->tail = (struct packet *) &buffer->data[length - split]; | |
205 | } | |
b5e47729 | 206 | |
28646821 SR |
207 | /* Finally, adjust buffer size and wake up userspace reader. */ |
208 | ||
b5e47729 | 209 | atomic_add(sizeof(struct packet) + length, &buffer->size); |
28646821 SR |
210 | wake_up_interruptible(&buffer->wait); |
211 | } | |
212 | ||
213 | static inline void | |
214 | reg_write(struct pcilynx *lynx, int offset, u32 data) | |
215 | { | |
b5e47729 | 216 | writel(data, lynx->registers + offset); |
28646821 SR |
217 | } |
218 | ||
219 | static inline u32 | |
220 | reg_read(struct pcilynx *lynx, int offset) | |
221 | { | |
b5e47729 | 222 | return readl(lynx->registers + offset); |
28646821 SR |
223 | } |
224 | ||
225 | static inline void | |
226 | reg_set_bits(struct pcilynx *lynx, int offset, u32 mask) | |
227 | { | |
b5e47729 | 228 | reg_write(lynx, offset, (reg_read(lynx, offset) | mask)); |
28646821 SR |
229 | } |
230 | ||
b5e47729 SR |
231 | /* |
232 | * Maybe the pcl programs could be set up to just append data instead | |
233 | * of using a whole packet. | |
234 | */ | |
235 | static inline void | |
236 | run_pcl(struct pcilynx *lynx, dma_addr_t pcl_bus, | |
237 | int dmachan) | |
28646821 | 238 | { |
b5e47729 SR |
239 | reg_write(lynx, DMA0_CURRENT_PCL + dmachan * 0x20, pcl_bus); |
240 | reg_write(lynx, DMA0_CHAN_CTRL + dmachan * 0x20, | |
241 | DMA_CHAN_CTRL_ENABLE | DMA_CHAN_CTRL_LINK); | |
28646821 SR |
242 | } |
243 | ||
244 | static int | |
245 | set_phy_reg(struct pcilynx *lynx, int addr, int val) | |
246 | { | |
b5e47729 | 247 | if (addr > 15) { |
7429b17d SR |
248 | dev_err(&lynx->pci_device->dev, |
249 | "PHY register address %d out of range\n", addr); | |
b5e47729 SR |
250 | return -1; |
251 | } | |
b5e47729 | 252 | if (val > 0xff) { |
7429b17d SR |
253 | dev_err(&lynx->pci_device->dev, |
254 | "PHY register value %d out of range\n", val); | |
b5e47729 SR |
255 | return -1; |
256 | } | |
b5e47729 | 257 | reg_write(lynx, LINK_PHY, LINK_PHY_WRITE | |
28646821 SR |
258 | LINK_PHY_ADDR(addr) | LINK_PHY_WDATA(val)); |
259 | ||
b5e47729 | 260 | return 0; |
28646821 SR |
261 | } |
262 | ||
55e77c06 SR |
263 | static int |
264 | nosy_open(struct inode *inode, struct file *file) | |
28646821 | 265 | { |
55e77c06 | 266 | int minor = iminor(inode); |
28646821 | 267 | struct client *client; |
424d66ce SR |
268 | struct pcilynx *tmp, *lynx = NULL; |
269 | ||
270 | mutex_lock(&card_mutex); | |
271 | list_for_each_entry(tmp, &card_list, link) | |
272 | if (tmp->misc.minor == minor) { | |
273 | lynx = lynx_get(tmp); | |
274 | break; | |
275 | } | |
276 | mutex_unlock(&card_mutex); | |
277 | if (lynx == NULL) | |
55e77c06 SR |
278 | return -ENODEV; |
279 | ||
28646821 | 280 | client = kmalloc(sizeof *client, GFP_KERNEL); |
55e77c06 | 281 | if (client == NULL) |
424d66ce | 282 | goto fail; |
55e77c06 | 283 | |
28646821 | 284 | client->tcode_mask = ~0; |
424d66ce | 285 | client->lynx = lynx; |
28646821 SR |
286 | INIT_LIST_HEAD(&client->link); |
287 | ||
424d66ce SR |
288 | if (packet_buffer_init(&client->buffer, 128 * 1024) < 0) |
289 | goto fail; | |
28646821 | 290 | |
55e77c06 | 291 | file->private_data = client; |
28646821 | 292 | |
c5bf68fe | 293 | return stream_open(inode, file); |
424d66ce SR |
294 | fail: |
295 | kfree(client); | |
296 | lynx_put(lynx); | |
297 | ||
298 | return -ENOMEM; | |
28646821 SR |
299 | } |
300 | ||
301 | static int | |
55e77c06 | 302 | nosy_release(struct inode *inode, struct file *file) |
28646821 | 303 | { |
55e77c06 | 304 | struct client *client = file->private_data; |
424d66ce | 305 | struct pcilynx *lynx = client->lynx; |
28646821 | 306 | |
424d66ce | 307 | spin_lock_irq(&lynx->client_list_lock); |
55e77c06 | 308 | list_del_init(&client->link); |
424d66ce | 309 | spin_unlock_irq(&lynx->client_list_lock); |
28646821 | 310 | |
55e77c06 SR |
311 | packet_buffer_destroy(&client->buffer); |
312 | kfree(client); | |
424d66ce | 313 | lynx_put(lynx); |
28646821 | 314 | |
b5e47729 | 315 | return 0; |
28646821 SR |
316 | } |
317 | ||
afc9a42b | 318 | static __poll_t |
28646821 SR |
319 | nosy_poll(struct file *file, poll_table *pt) |
320 | { | |
321 | struct client *client = file->private_data; | |
afc9a42b | 322 | __poll_t ret = 0; |
28646821 SR |
323 | |
324 | poll_wait(file, &client->buffer.wait, pt); | |
325 | ||
326 | if (atomic_read(&client->buffer.size) > 0) | |
a9a08845 | 327 | ret = EPOLLIN | EPOLLRDNORM; |
424d66ce SR |
328 | |
329 | if (list_empty(&client->lynx->link)) | |
a9a08845 | 330 | ret |= EPOLLHUP; |
424d66ce SR |
331 | |
332 | return ret; | |
28646821 SR |
333 | } |
334 | ||
335 | static ssize_t | |
c89db7b8 | 336 | nosy_read(struct file *file, char __user *buffer, size_t count, loff_t *offset) |
28646821 SR |
337 | { |
338 | struct client *client = file->private_data; | |
339 | ||
424d66ce | 340 | return packet_buffer_get(client, buffer, count); |
28646821 SR |
341 | } |
342 | ||
c7b2a99c SR |
343 | static long |
344 | nosy_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
28646821 SR |
345 | { |
346 | struct client *client = file->private_data; | |
c7b2a99c | 347 | spinlock_t *client_list_lock = &client->lynx->client_list_lock; |
b5e47729 | 348 | struct nosy_stats stats; |
e186dc58 | 349 | int ret; |
28646821 SR |
350 | |
351 | switch (cmd) { | |
b5e47729 | 352 | case NOSY_IOC_GET_STATS: |
c7b2a99c | 353 | spin_lock_irq(client_list_lock); |
28646821 | 354 | stats.total_packet_count = client->buffer.total_packet_count; |
c7b2a99c SR |
355 | stats.lost_packet_count = client->buffer.lost_packet_count; |
356 | spin_unlock_irq(client_list_lock); | |
357 | ||
c89db7b8 | 358 | if (copy_to_user((void __user *) arg, &stats, sizeof stats)) |
28646821 SR |
359 | return -EFAULT; |
360 | else | |
361 | return 0; | |
b5e47729 | 362 | |
28646821 | 363 | case NOSY_IOC_START: |
e186dc58 | 364 | ret = -EBUSY; |
55e77c06 | 365 | spin_lock_irq(client_list_lock); |
e186dc58 ZM |
366 | if (list_empty(&client->link)) { |
367 | list_add_tail(&client->link, &client->lynx->client_list); | |
368 | ret = 0; | |
369 | } | |
55e77c06 SR |
370 | spin_unlock_irq(client_list_lock); |
371 | ||
e186dc58 | 372 | return ret; |
28646821 SR |
373 | |
374 | case NOSY_IOC_STOP: | |
55e77c06 SR |
375 | spin_lock_irq(client_list_lock); |
376 | list_del_init(&client->link); | |
377 | spin_unlock_irq(client_list_lock); | |
378 | ||
28646821 SR |
379 | return 0; |
380 | ||
381 | case NOSY_IOC_FILTER: | |
c7b2a99c | 382 | spin_lock_irq(client_list_lock); |
28646821 | 383 | client->tcode_mask = arg; |
c7b2a99c | 384 | spin_unlock_irq(client_list_lock); |
55e77c06 | 385 | |
28646821 SR |
386 | return 0; |
387 | ||
388 | default: | |
389 | return -EINVAL; | |
390 | /* Flush buffer, configure filter. */ | |
391 | } | |
392 | } | |
393 | ||
b5e47729 | 394 | static const struct file_operations nosy_ops = { |
c7b2a99c SR |
395 | .owner = THIS_MODULE, |
396 | .read = nosy_read, | |
397 | .unlocked_ioctl = nosy_ioctl, | |
398 | .poll = nosy_poll, | |
399 | .open = nosy_open, | |
400 | .release = nosy_release, | |
28646821 SR |
401 | }; |
402 | ||
403 | #define PHY_PACKET_SIZE 12 /* 1 payload, 1 inverse, 1 ack = 3 quadlets */ | |
404 | ||
28646821 | 405 | static void |
685c3f80 | 406 | packet_irq_handler(struct pcilynx *lynx) |
28646821 | 407 | { |
28646821 | 408 | struct client *client; |
2ae4b6b2 | 409 | u32 tcode_mask, tcode, timestamp; |
28646821 | 410 | size_t length; |
2ae4b6b2 | 411 | struct timespec64 ts64; |
28646821 SR |
412 | |
413 | /* FIXME: Also report rcv_speed. */ | |
414 | ||
fd8c8d46 SR |
415 | length = __le32_to_cpu(lynx->rcv_pcl->pcl_status) & 0x00001fff; |
416 | tcode = __le32_to_cpu(lynx->rcv_buffer[1]) >> 4 & 0xf; | |
28646821 | 417 | |
2ae4b6b2 AKC |
418 | ktime_get_real_ts64(&ts64); |
419 | timestamp = ts64.tv_nsec / NSEC_PER_USEC; | |
420 | lynx->rcv_buffer[0] = (__force __le32)timestamp; | |
28646821 SR |
421 | |
422 | if (length == PHY_PACKET_SIZE) | |
423 | tcode_mask = 1 << TCODE_PHY_PACKET; | |
424 | else | |
fd8c8d46 | 425 | tcode_mask = 1 << tcode; |
28646821 | 426 | |
685c3f80 | 427 | spin_lock(&lynx->client_list_lock); |
28646821 | 428 | |
b5e47729 | 429 | list_for_each_entry(client, &lynx->client_list, link) |
28646821 | 430 | if (client->tcode_mask & tcode_mask) |
b5e47729 | 431 | packet_buffer_put(&client->buffer, |
28646821 | 432 | lynx->rcv_buffer, length + 4); |
28646821 | 433 | |
685c3f80 | 434 | spin_unlock(&lynx->client_list_lock); |
28646821 SR |
435 | } |
436 | ||
437 | static void | |
685c3f80 | 438 | bus_reset_irq_handler(struct pcilynx *lynx) |
28646821 | 439 | { |
28646821 | 440 | struct client *client; |
384fbb96 TR |
441 | struct timespec64 ts64; |
442 | u32 timestamp; | |
28646821 | 443 | |
384fbb96 TR |
444 | ktime_get_real_ts64(&ts64); |
445 | timestamp = ts64.tv_nsec / NSEC_PER_USEC; | |
28646821 | 446 | |
685c3f80 | 447 | spin_lock(&lynx->client_list_lock); |
28646821 | 448 | |
b5e47729 | 449 | list_for_each_entry(client, &lynx->client_list, link) |
384fbb96 | 450 | packet_buffer_put(&client->buffer, ×tamp, 4); |
28646821 | 451 | |
685c3f80 | 452 | spin_unlock(&lynx->client_list_lock); |
28646821 SR |
453 | } |
454 | ||
28646821 SR |
455 | static irqreturn_t |
456 | irq_handler(int irq, void *device) | |
457 | { | |
b5e47729 | 458 | struct pcilynx *lynx = device; |
28646821 | 459 | u32 pci_int_status; |
b5e47729 SR |
460 | |
461 | pci_int_status = reg_read(lynx, PCI_INT_STATUS); | |
28646821 | 462 | |
16547667 SR |
463 | if (pci_int_status == ~0) |
464 | /* Card was ejected. */ | |
465 | return IRQ_NONE; | |
466 | ||
28646821 SR |
467 | if ((pci_int_status & PCI_INT_INT_PEND) == 0) |
468 | /* Not our interrupt, bail out quickly. */ | |
469 | return IRQ_NONE; | |
470 | ||
471 | if ((pci_int_status & PCI_INT_P1394_INT) != 0) { | |
472 | u32 link_int_status; | |
473 | ||
474 | link_int_status = reg_read(lynx, LINK_INT_STATUS); | |
475 | reg_write(lynx, LINK_INT_STATUS, link_int_status); | |
476 | ||
477 | if ((link_int_status & LINK_INT_PHY_BUSRESET) > 0) | |
685c3f80 | 478 | bus_reset_irq_handler(lynx); |
28646821 SR |
479 | } |
480 | ||
481 | /* Clear the PCI_INT_STATUS register only after clearing the | |
482 | * LINK_INT_STATUS register; otherwise the PCI_INT_P1394 will | |
483 | * be set again immediately. */ | |
484 | ||
485 | reg_write(lynx, PCI_INT_STATUS, pci_int_status); | |
486 | ||
487 | if ((pci_int_status & PCI_INT_DMA0_HLT) > 0) { | |
685c3f80 | 488 | packet_irq_handler(lynx); |
28646821 SR |
489 | run_pcl(lynx, lynx->rcv_start_pcl_bus, 0); |
490 | } | |
491 | ||
492 | return IRQ_HANDLED; | |
493 | } | |
494 | ||
495 | static void | |
496 | remove_card(struct pci_dev *dev) | |
497 | { | |
424d66ce SR |
498 | struct pcilynx *lynx = pci_get_drvdata(dev); |
499 | struct client *client; | |
28646821 | 500 | |
424d66ce SR |
501 | mutex_lock(&card_mutex); |
502 | list_del_init(&lynx->link); | |
503 | misc_deregister(&lynx->misc); | |
504 | mutex_unlock(&card_mutex); | |
28646821 SR |
505 | |
506 | reg_write(lynx, PCI_INT_ENABLE, 0); | |
507 | free_irq(lynx->pci_device->irq, lynx); | |
508 | ||
424d66ce SR |
509 | spin_lock_irq(&lynx->client_list_lock); |
510 | list_for_each_entry(client, &lynx->client_list, link) | |
511 | wake_up_interruptible(&client->buffer.wait); | |
512 | spin_unlock_irq(&lynx->client_list_lock); | |
513 | ||
b5e47729 | 514 | pci_free_consistent(lynx->pci_device, sizeof(struct pcl), |
28646821 | 515 | lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus); |
b5e47729 | 516 | pci_free_consistent(lynx->pci_device, sizeof(struct pcl), |
28646821 SR |
517 | lynx->rcv_pcl, lynx->rcv_pcl_bus); |
518 | pci_free_consistent(lynx->pci_device, PAGE_SIZE, | |
519 | lynx->rcv_buffer, lynx->rcv_buffer_bus); | |
520 | ||
521 | iounmap(lynx->registers); | |
b6d9c125 | 522 | pci_disable_device(dev); |
424d66ce | 523 | lynx_put(lynx); |
28646821 SR |
524 | } |
525 | ||
526 | #define RCV_BUFFER_SIZE (16 * 1024) | |
527 | ||
03f94c0f | 528 | static int |
28646821 SR |
529 | add_card(struct pci_dev *dev, const struct pci_device_id *unused) |
530 | { | |
b5e47729 | 531 | struct pcilynx *lynx; |
28646821 | 532 | u32 p, end; |
b6d9c125 | 533 | int ret, i; |
28646821 | 534 | |
e894d1d7 | 535 | if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) { |
7429b17d SR |
536 | dev_err(&dev->dev, |
537 | "DMA address limits not supported for PCILynx hardware\n"); | |
b5e47729 SR |
538 | return -ENXIO; |
539 | } | |
540 | if (pci_enable_device(dev)) { | |
7429b17d | 541 | dev_err(&dev->dev, "Failed to enable PCILynx hardware\n"); |
b5e47729 SR |
542 | return -ENXIO; |
543 | } | |
544 | pci_set_master(dev); | |
28646821 SR |
545 | |
546 | lynx = kzalloc(sizeof *lynx, GFP_KERNEL); | |
b5e47729 | 547 | if (lynx == NULL) { |
7429b17d | 548 | dev_err(&dev->dev, "Failed to allocate control structure\n"); |
b6d9c125 SR |
549 | ret = -ENOMEM; |
550 | goto fail_disable; | |
b5e47729 SR |
551 | } |
552 | lynx->pci_device = dev; | |
553 | pci_set_drvdata(dev, lynx); | |
28646821 SR |
554 | |
555 | spin_lock_init(&lynx->client_list_lock); | |
556 | INIT_LIST_HEAD(&lynx->client_list); | |
424d66ce | 557 | kref_init(&lynx->kref); |
28646821 | 558 | |
b5e47729 SR |
559 | lynx->registers = ioremap_nocache(pci_resource_start(dev, 0), |
560 | PCILYNX_MAX_REGISTER); | |
6449e31d AK |
561 | if (lynx->registers == NULL) { |
562 | dev_err(&dev->dev, "Failed to map registers\n"); | |
563 | ret = -ENOMEM; | |
564 | goto fail_deallocate_lynx; | |
565 | } | |
b5e47729 SR |
566 | |
567 | lynx->rcv_start_pcl = pci_alloc_consistent(lynx->pci_device, | |
568 | sizeof(struct pcl), &lynx->rcv_start_pcl_bus); | |
569 | lynx->rcv_pcl = pci_alloc_consistent(lynx->pci_device, | |
570 | sizeof(struct pcl), &lynx->rcv_pcl_bus); | |
571 | lynx->rcv_buffer = pci_alloc_consistent(lynx->pci_device, | |
572 | RCV_BUFFER_SIZE, &lynx->rcv_buffer_bus); | |
573 | if (lynx->rcv_start_pcl == NULL || | |
28646821 | 574 | lynx->rcv_pcl == NULL || |
b5e47729 | 575 | lynx->rcv_buffer == NULL) { |
7429b17d | 576 | dev_err(&dev->dev, "Failed to allocate receive buffer\n"); |
b6d9c125 | 577 | ret = -ENOMEM; |
6449e31d | 578 | goto fail_deallocate_buffers; |
b5e47729 | 579 | } |
fd8c8d46 SR |
580 | lynx->rcv_start_pcl->next = cpu_to_le32(lynx->rcv_pcl_bus); |
581 | lynx->rcv_pcl->next = cpu_to_le32(PCL_NEXT_INVALID); | |
582 | lynx->rcv_pcl->async_error_next = cpu_to_le32(PCL_NEXT_INVALID); | |
28646821 | 583 | |
b5e47729 | 584 | lynx->rcv_pcl->buffer[0].control = |
fd8c8d46 SR |
585 | cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2044); |
586 | lynx->rcv_pcl->buffer[0].pointer = | |
587 | cpu_to_le32(lynx->rcv_buffer_bus + 4); | |
28646821 SR |
588 | p = lynx->rcv_buffer_bus + 2048; |
589 | end = lynx->rcv_buffer_bus + RCV_BUFFER_SIZE; | |
590 | for (i = 1; p < end; i++, p += 2048) { | |
591 | lynx->rcv_pcl->buffer[i].control = | |
fd8c8d46 SR |
592 | cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2048); |
593 | lynx->rcv_pcl->buffer[i].pointer = cpu_to_le32(p); | |
28646821 | 594 | } |
fd8c8d46 | 595 | lynx->rcv_pcl->buffer[i - 1].control |= cpu_to_le32(PCL_LAST_BUFF); |
28646821 | 596 | |
b5e47729 SR |
597 | reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET); |
598 | /* Fix buggy cards with autoboot pin not tied low: */ | |
599 | reg_write(lynx, DMA0_CHAN_CTRL, 0); | |
600 | reg_write(lynx, DMA_GLOBAL_REGISTER, 0x00 << 24); | |
28646821 SR |
601 | |
602 | #if 0 | |
b5e47729 SR |
603 | /* now, looking for PHY register set */ |
604 | if ((get_phy_reg(lynx, 2) & 0xe0) == 0xe0) { | |
605 | lynx->phyic.reg_1394a = 1; | |
606 | PRINT(KERN_INFO, lynx->id, | |
607 | "found 1394a conform PHY (using extended register set)"); | |
608 | lynx->phyic.vendor = get_phy_vendorid(lynx); | |
609 | lynx->phyic.product = get_phy_productid(lynx); | |
610 | } else { | |
611 | lynx->phyic.reg_1394a = 0; | |
612 | PRINT(KERN_INFO, lynx->id, "found old 1394 PHY"); | |
613 | } | |
28646821 SR |
614 | #endif |
615 | ||
616 | /* Setup the general receive FIFO max size. */ | |
617 | reg_write(lynx, FIFO_SIZES, 255); | |
618 | ||
b5e47729 | 619 | reg_set_bits(lynx, PCI_INT_ENABLE, PCI_INT_DMA_ALL); |
28646821 | 620 | |
b5e47729 | 621 | reg_write(lynx, LINK_INT_ENABLE, |
28646821 SR |
622 | LINK_INT_PHY_TIME_OUT | LINK_INT_PHY_REG_RCVD | |
623 | LINK_INT_PHY_BUSRESET | LINK_INT_IT_STUCK | | |
624 | LINK_INT_AT_STUCK | LINK_INT_SNTRJ | | |
625 | LINK_INT_TC_ERR | LINK_INT_GRF_OVER_FLOW | | |
626 | LINK_INT_ITF_UNDER_FLOW | LINK_INT_ATF_UNDER_FLOW); | |
627 | ||
628 | /* Disable the L flag in self ID packets. */ | |
629 | set_phy_reg(lynx, 4, 0); | |
630 | ||
631 | /* Put this baby into snoop mode */ | |
632 | reg_set_bits(lynx, LINK_CONTROL, LINK_CONTROL_SNOOP_ENABLE); | |
633 | ||
634 | run_pcl(lynx, lynx->rcv_start_pcl_bus, 0); | |
635 | ||
b5e47729 SR |
636 | if (request_irq(dev->irq, irq_handler, IRQF_SHARED, |
637 | driver_name, lynx)) { | |
7429b17d SR |
638 | dev_err(&dev->dev, |
639 | "Failed to allocate shared interrupt %d\n", dev->irq); | |
b6d9c125 | 640 | ret = -EIO; |
6449e31d | 641 | goto fail_deallocate_buffers; |
b5e47729 | 642 | } |
28646821 SR |
643 | |
644 | lynx->misc.parent = &dev->dev; | |
645 | lynx->misc.minor = MISC_DYNAMIC_MINOR; | |
646 | lynx->misc.name = "nosy"; | |
647 | lynx->misc.fops = &nosy_ops; | |
424d66ce SR |
648 | |
649 | mutex_lock(&card_mutex); | |
b6d9c125 SR |
650 | ret = misc_register(&lynx->misc); |
651 | if (ret) { | |
7429b17d | 652 | dev_err(&dev->dev, "Failed to register misc char device\n"); |
424d66ce | 653 | mutex_unlock(&card_mutex); |
b6d9c125 | 654 | goto fail_free_irq; |
b5e47729 | 655 | } |
424d66ce SR |
656 | list_add_tail(&lynx->link, &card_list); |
657 | mutex_unlock(&card_mutex); | |
28646821 | 658 | |
7429b17d SR |
659 | dev_info(&dev->dev, |
660 | "Initialized PCILynx IEEE1394 card, irq=%d\n", dev->irq); | |
28646821 | 661 | |
b5e47729 | 662 | return 0; |
b6d9c125 SR |
663 | |
664 | fail_free_irq: | |
665 | reg_write(lynx, PCI_INT_ENABLE, 0); | |
666 | free_irq(lynx->pci_device->irq, lynx); | |
667 | ||
6449e31d | 668 | fail_deallocate_buffers: |
b6d9c125 SR |
669 | if (lynx->rcv_start_pcl) |
670 | pci_free_consistent(lynx->pci_device, sizeof(struct pcl), | |
671 | lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus); | |
672 | if (lynx->rcv_pcl) | |
673 | pci_free_consistent(lynx->pci_device, sizeof(struct pcl), | |
674 | lynx->rcv_pcl, lynx->rcv_pcl_bus); | |
675 | if (lynx->rcv_buffer) | |
676 | pci_free_consistent(lynx->pci_device, PAGE_SIZE, | |
677 | lynx->rcv_buffer, lynx->rcv_buffer_bus); | |
678 | iounmap(lynx->registers); | |
6449e31d AK |
679 | |
680 | fail_deallocate_lynx: | |
b6d9c125 SR |
681 | kfree(lynx); |
682 | ||
683 | fail_disable: | |
684 | pci_disable_device(dev); | |
685 | ||
686 | return ret; | |
28646821 SR |
687 | } |
688 | ||
7eeb7418 | 689 | static struct pci_device_id pci_table[] = { |
28646821 | 690 | { |
b5e47729 SR |
691 | .vendor = PCI_VENDOR_ID_TI, |
692 | .device = PCI_DEVICE_ID_TI_PCILYNX, | |
693 | .subvendor = PCI_ANY_ID, | |
694 | .subdevice = PCI_ANY_ID, | |
28646821 SR |
695 | }, |
696 | { } /* Terminating entry */ | |
697 | }; | |
698 | ||
fe2af11c AL |
699 | MODULE_DEVICE_TABLE(pci, pci_table); |
700 | ||
28646821 | 701 | static struct pci_driver lynx_pci_driver = { |
b5e47729 | 702 | .name = driver_name, |
28646821 SR |
703 | .id_table = pci_table, |
704 | .probe = add_card, | |
b5e47729 | 705 | .remove = remove_card, |
28646821 SR |
706 | }; |
707 | ||
fe2af11c AL |
708 | module_pci_driver(lynx_pci_driver); |
709 | ||
b5e47729 | 710 | MODULE_AUTHOR("Kristian Hoegsberg"); |
28646821 SR |
711 | MODULE_DESCRIPTION("Snoop mode driver for TI pcilynx 1394 controllers"); |
712 | MODULE_LICENSE("GPL"); |