Commit | Line | Data |
---|---|---|
b5e47729 SR |
1 | /* |
2 | * nosy - Snoop mode driver for TI PCILynx 1394 controllers | |
3 | * Copyright (C) 2002-2007 Kristian Høgsberg | |
28646821 SR |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software Foundation, | |
17 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
18 | */ | |
19 | ||
28646821 | 20 | #include <linux/errno.h> |
b5e47729 | 21 | #include <linux/fs.h> |
28646821 | 22 | #include <linux/init.h> |
b5e47729 SR |
23 | #include <linux/interrupt.h> |
24 | #include <linux/io.h> | |
25 | #include <linux/kernel.h> | |
26 | #include <linux/miscdevice.h> | |
27 | #include <linux/module.h> | |
28646821 | 28 | #include <linux/pci.h> |
28646821 | 29 | #include <linux/poll.h> |
b5e47729 SR |
30 | #include <linux/sched.h> /* required for linux/wait.h */ |
31 | #include <linux/slab.h> | |
32 | #include <linux/spinlock.h> | |
33 | #include <linux/timex.h> | |
34 | #include <linux/uaccess.h> | |
35 | #include <linux/wait.h> | |
36 | ||
28646821 | 37 | #include <asm/atomic.h> |
b5e47729 | 38 | #include <asm/byteorder.h> |
28646821 SR |
39 | |
40 | #include "nosy.h" | |
41 | #include "nosy-user.h" | |
42 | ||
43 | #define TCODE_PHY_PACKET 0x10 | |
44 | #define PCI_DEVICE_ID_TI_PCILYNX 0x8000 | |
45 | ||
46 | #define notify(s, args...) printk(KERN_NOTICE s, ## args) | |
47 | #define error(s, args...) printk(KERN_ERR s, ## args) | |
48 | #define debug(s, args...) printk(KERN_DEBUG s, ## args) | |
49 | ||
b5e47729 | 50 | static char driver_name[] = KBUILD_MODNAME; |
28646821 SR |
51 | |
52 | struct pcl_status { | |
53 | unsigned int transfer_count : 13; | |
54 | unsigned int reserved0 : 1; | |
55 | unsigned int ack_type : 1; | |
56 | unsigned int ack : 4; | |
57 | unsigned int rcv_speed : 2; | |
58 | unsigned int rcv_dma_channel : 6; | |
59 | unsigned int packet_complete : 1; | |
60 | unsigned int packet_error : 1; | |
61 | unsigned int master_error : 1; | |
62 | unsigned int iso_mode : 1; | |
63 | unsigned int self_id : 1; | |
64 | }; | |
65 | ||
66 | /* this is the physical layout of a PCL, its size is 128 bytes */ | |
67 | struct pcl { | |
68 | u32 next; | |
69 | u32 async_error_next; | |
70 | u32 user_data; | |
71 | struct pcl_status pcl_status; | |
72 | u32 remaining_transfer_count; | |
73 | u32 next_data_buffer; | |
74 | struct { | |
75 | u32 control; | |
76 | u32 pointer; | |
77 | } buffer[13] __attribute__ ((packed)); | |
78 | } __attribute__ ((packed)); | |
79 | ||
80 | struct packet { | |
b5e47729 | 81 | unsigned int length; |
28646821 SR |
82 | char data[0]; |
83 | }; | |
84 | ||
85 | struct packet_buffer { | |
86 | char *data; | |
87 | size_t capacity; | |
88 | long total_packet_count, lost_packet_count; | |
89 | atomic_t size; | |
b5e47729 SR |
90 | struct packet *head, *tail; |
91 | wait_queue_head_t wait; | |
28646821 SR |
92 | }; |
93 | ||
94 | struct pcilynx { | |
95 | struct pci_dev *pci_device; | |
96 | unsigned char *registers; | |
97 | ||
98 | struct pcl *rcv_start_pcl, *rcv_pcl; | |
99 | u32 *rcv_buffer; | |
100 | ||
101 | dma_addr_t rcv_start_pcl_bus, rcv_pcl_bus, rcv_buffer_bus; | |
102 | ||
103 | spinlock_t client_list_lock; | |
104 | struct list_head client_list; | |
105 | ||
106 | struct miscdevice misc; | |
107 | }; | |
108 | ||
28646821 SR |
109 | struct client { |
110 | struct pcilynx *lynx; | |
c7b2a99c | 111 | u32 tcode_mask; |
28646821 SR |
112 | struct packet_buffer buffer; |
113 | struct list_head link; | |
114 | }; | |
115 | ||
116 | #define MAX_MINORS 64 | |
b5e47729 | 117 | static struct pcilynx *minors[MAX_MINORS]; |
28646821 SR |
118 | |
119 | static int | |
120 | packet_buffer_init(struct packet_buffer *buffer, size_t capacity) | |
121 | { | |
122 | buffer->data = kmalloc(capacity, GFP_KERNEL); | |
123 | if (buffer->data == NULL) | |
124 | return -ENOMEM; | |
125 | buffer->head = (struct packet *) buffer->data; | |
126 | buffer->tail = (struct packet *) buffer->data; | |
127 | buffer->capacity = capacity; | |
128 | buffer->lost_packet_count = 0; | |
129 | atomic_set(&buffer->size, 0); | |
b5e47729 | 130 | init_waitqueue_head(&buffer->wait); |
28646821 SR |
131 | |
132 | return 0; | |
133 | } | |
134 | ||
135 | static void | |
136 | packet_buffer_destroy(struct packet_buffer *buffer) | |
137 | { | |
138 | kfree(buffer->data); | |
139 | } | |
140 | ||
141 | static int | |
142 | packet_buffer_get(struct packet_buffer *buffer, void *data, size_t user_length) | |
143 | { | |
144 | size_t length; | |
145 | char *end; | |
146 | ||
147 | if (wait_event_interruptible(buffer->wait, | |
148 | atomic_read(&buffer->size) > 0)) | |
149 | return -ERESTARTSYS; | |
150 | ||
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 SR |
247 | if (addr > 15) { |
248 | debug("PHY register address %d out of range\n", addr); | |
249 | return -1; | |
250 | } | |
251 | ||
252 | if (val > 0xff) { | |
253 | debug("PHY register value %d out of range\n", val); | |
254 | return -1; | |
255 | } | |
256 | ||
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 | ||
263 | static void | |
264 | nosy_start_snoop(struct client *client) | |
265 | { | |
685c3f80 | 266 | spin_lock_irq(&client->lynx->client_list_lock); |
28646821 | 267 | list_add_tail(&client->link, &client->lynx->client_list); |
685c3f80 | 268 | spin_unlock_irq(&client->lynx->client_list_lock); |
28646821 SR |
269 | } |
270 | ||
271 | static void | |
272 | nosy_stop_snoop(struct client *client) | |
273 | { | |
685c3f80 | 274 | spin_lock_irq(&client->lynx->client_list_lock); |
a2d39db9 | 275 | list_del_init(&client->link); |
685c3f80 | 276 | spin_unlock_irq(&client->lynx->client_list_lock); |
28646821 SR |
277 | } |
278 | ||
279 | static struct client * | |
280 | nosy_add_client(struct pcilynx *lynx) | |
281 | { | |
282 | struct client *client; | |
283 | ||
284 | client = kmalloc(sizeof *client, GFP_KERNEL); | |
285 | client->tcode_mask = ~0; | |
286 | client->lynx = lynx; | |
287 | INIT_LIST_HEAD(&client->link); | |
288 | ||
289 | if (packet_buffer_init(&client->buffer, 128 * 1024) < 0) { | |
290 | kfree(client); | |
291 | debug("Failed to allocate packet buffer\n"); | |
292 | return NULL; | |
293 | } | |
294 | ||
295 | return client; | |
296 | } | |
297 | ||
298 | static void | |
299 | nosy_remove_client(struct client *client) | |
300 | { | |
301 | nosy_stop_snoop(client); | |
302 | packet_buffer_destroy(&client->buffer); | |
303 | kfree(client); | |
304 | } | |
305 | ||
306 | static int | |
307 | nosy_open(struct inode *inode, struct file *file) | |
308 | { | |
309 | int minor = iminor(inode); | |
310 | ||
311 | if (minor > MAX_MINORS || minors[minor] == NULL) | |
312 | return -ENODEV; | |
313 | ||
b5e47729 | 314 | file->private_data = nosy_add_client(minors[minor]); |
28646821 SR |
315 | if (file->private_data == NULL) |
316 | return -ENOMEM; | |
317 | else | |
318 | return 0; | |
319 | } | |
320 | ||
321 | static int | |
322 | nosy_release(struct inode *inode, struct file *file) | |
323 | { | |
324 | nosy_remove_client(file->private_data); | |
325 | ||
b5e47729 | 326 | return 0; |
28646821 SR |
327 | } |
328 | ||
329 | static unsigned int | |
330 | nosy_poll(struct file *file, poll_table *pt) | |
331 | { | |
332 | struct client *client = file->private_data; | |
333 | ||
334 | poll_wait(file, &client->buffer.wait, pt); | |
335 | ||
336 | if (atomic_read(&client->buffer.size) > 0) | |
337 | return POLLIN | POLLRDNORM; | |
338 | else | |
339 | return 0; | |
340 | } | |
341 | ||
342 | static ssize_t | |
343 | nosy_read(struct file *file, char *buffer, size_t count, loff_t *offset) | |
344 | { | |
345 | struct client *client = file->private_data; | |
346 | ||
347 | return packet_buffer_get(&client->buffer, buffer, count); | |
348 | } | |
349 | ||
c7b2a99c SR |
350 | static long |
351 | nosy_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
28646821 SR |
352 | { |
353 | struct client *client = file->private_data; | |
c7b2a99c | 354 | spinlock_t *client_list_lock = &client->lynx->client_list_lock; |
b5e47729 | 355 | struct nosy_stats stats; |
28646821 SR |
356 | |
357 | switch (cmd) { | |
b5e47729 | 358 | case NOSY_IOC_GET_STATS: |
c7b2a99c | 359 | spin_lock_irq(client_list_lock); |
28646821 | 360 | stats.total_packet_count = client->buffer.total_packet_count; |
c7b2a99c SR |
361 | stats.lost_packet_count = client->buffer.lost_packet_count; |
362 | spin_unlock_irq(client_list_lock); | |
363 | ||
28646821 SR |
364 | if (copy_to_user((void *) arg, &stats, sizeof stats)) |
365 | return -EFAULT; | |
366 | else | |
367 | return 0; | |
b5e47729 | 368 | |
28646821 SR |
369 | case NOSY_IOC_START: |
370 | nosy_start_snoop(client); | |
371 | return 0; | |
372 | ||
373 | case NOSY_IOC_STOP: | |
374 | nosy_stop_snoop(client); | |
375 | return 0; | |
376 | ||
377 | case NOSY_IOC_FILTER: | |
c7b2a99c | 378 | spin_lock_irq(client_list_lock); |
28646821 | 379 | client->tcode_mask = arg; |
c7b2a99c | 380 | spin_unlock_irq(client_list_lock); |
28646821 SR |
381 | return 0; |
382 | ||
383 | default: | |
384 | return -EINVAL; | |
385 | /* Flush buffer, configure filter. */ | |
386 | } | |
387 | } | |
388 | ||
b5e47729 | 389 | static const struct file_operations nosy_ops = { |
c7b2a99c SR |
390 | .owner = THIS_MODULE, |
391 | .read = nosy_read, | |
392 | .unlocked_ioctl = nosy_ioctl, | |
393 | .poll = nosy_poll, | |
394 | .open = nosy_open, | |
395 | .release = nosy_release, | |
28646821 SR |
396 | }; |
397 | ||
398 | #define PHY_PACKET_SIZE 12 /* 1 payload, 1 inverse, 1 ack = 3 quadlets */ | |
399 | ||
400 | struct link_packet { | |
401 | unsigned int priority : 4; | |
402 | unsigned int tcode : 4; | |
403 | unsigned int rt : 2; | |
404 | unsigned int tlabel : 6; | |
405 | unsigned int destination : 16; | |
406 | }; | |
407 | ||
408 | static void | |
685c3f80 | 409 | packet_irq_handler(struct pcilynx *lynx) |
28646821 | 410 | { |
28646821 | 411 | struct client *client; |
c7b2a99c | 412 | u32 tcode_mask; |
28646821 SR |
413 | size_t length; |
414 | struct link_packet *packet; | |
415 | struct timeval tv; | |
416 | ||
417 | /* FIXME: Also report rcv_speed. */ | |
418 | ||
419 | length = lynx->rcv_pcl->pcl_status.transfer_count; | |
420 | packet = (struct link_packet *) &lynx->rcv_buffer[1]; | |
421 | ||
422 | do_gettimeofday(&tv); | |
423 | lynx->rcv_buffer[0] = tv.tv_usec; | |
424 | ||
425 | if (length == PHY_PACKET_SIZE) | |
426 | tcode_mask = 1 << TCODE_PHY_PACKET; | |
427 | else | |
428 | tcode_mask = 1 << packet->tcode; | |
429 | ||
685c3f80 | 430 | spin_lock(&lynx->client_list_lock); |
28646821 | 431 | |
b5e47729 | 432 | list_for_each_entry(client, &lynx->client_list, link) |
28646821 | 433 | if (client->tcode_mask & tcode_mask) |
b5e47729 | 434 | packet_buffer_put(&client->buffer, |
28646821 | 435 | lynx->rcv_buffer, length + 4); |
28646821 | 436 | |
685c3f80 | 437 | spin_unlock(&lynx->client_list_lock); |
28646821 SR |
438 | } |
439 | ||
440 | static void | |
685c3f80 | 441 | bus_reset_irq_handler(struct pcilynx *lynx) |
28646821 | 442 | { |
28646821 SR |
443 | struct client *client; |
444 | struct timeval tv; | |
445 | ||
446 | do_gettimeofday(&tv); | |
447 | ||
685c3f80 | 448 | spin_lock(&lynx->client_list_lock); |
28646821 | 449 | |
b5e47729 | 450 | list_for_each_entry(client, &lynx->client_list, link) |
28646821 | 451 | packet_buffer_put(&client->buffer, &tv.tv_usec, 4); |
28646821 | 452 | |
685c3f80 | 453 | spin_unlock(&lynx->client_list_lock); |
28646821 SR |
454 | } |
455 | ||
28646821 SR |
456 | static irqreturn_t |
457 | irq_handler(int irq, void *device) | |
458 | { | |
b5e47729 | 459 | struct pcilynx *lynx = device; |
28646821 | 460 | u32 pci_int_status; |
b5e47729 SR |
461 | |
462 | pci_int_status = reg_read(lynx, PCI_INT_STATUS); | |
28646821 SR |
463 | |
464 | if ((pci_int_status & PCI_INT_INT_PEND) == 0) | |
465 | /* Not our interrupt, bail out quickly. */ | |
466 | return IRQ_NONE; | |
467 | ||
468 | if ((pci_int_status & PCI_INT_P1394_INT) != 0) { | |
469 | u32 link_int_status; | |
470 | ||
471 | link_int_status = reg_read(lynx, LINK_INT_STATUS); | |
472 | reg_write(lynx, LINK_INT_STATUS, link_int_status); | |
473 | ||
474 | if ((link_int_status & LINK_INT_PHY_BUSRESET) > 0) | |
685c3f80 | 475 | bus_reset_irq_handler(lynx); |
28646821 SR |
476 | } |
477 | ||
478 | /* Clear the PCI_INT_STATUS register only after clearing the | |
479 | * LINK_INT_STATUS register; otherwise the PCI_INT_P1394 will | |
480 | * be set again immediately. */ | |
481 | ||
482 | reg_write(lynx, PCI_INT_STATUS, pci_int_status); | |
483 | ||
484 | if ((pci_int_status & PCI_INT_DMA0_HLT) > 0) { | |
685c3f80 | 485 | packet_irq_handler(lynx); |
28646821 SR |
486 | run_pcl(lynx, lynx->rcv_start_pcl_bus, 0); |
487 | } | |
488 | ||
489 | return IRQ_HANDLED; | |
490 | } | |
491 | ||
492 | static void | |
493 | remove_card(struct pci_dev *dev) | |
494 | { | |
b5e47729 | 495 | struct pcilynx *lynx; |
28646821 | 496 | |
b5e47729 SR |
497 | lynx = pci_get_drvdata(dev); |
498 | if (!lynx) | |
28646821 | 499 | return; |
b5e47729 | 500 | pci_set_drvdata(dev, NULL); |
28646821 SR |
501 | |
502 | reg_write(lynx, PCI_INT_ENABLE, 0); | |
503 | free_irq(lynx->pci_device->irq, lynx); | |
504 | ||
b5e47729 | 505 | pci_free_consistent(lynx->pci_device, sizeof(struct pcl), |
28646821 | 506 | lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus); |
b5e47729 | 507 | pci_free_consistent(lynx->pci_device, sizeof(struct pcl), |
28646821 SR |
508 | lynx->rcv_pcl, lynx->rcv_pcl_bus); |
509 | pci_free_consistent(lynx->pci_device, PAGE_SIZE, | |
510 | lynx->rcv_buffer, lynx->rcv_buffer_bus); | |
511 | ||
512 | iounmap(lynx->registers); | |
513 | ||
514 | minors[lynx->misc.minor] = NULL; | |
515 | misc_deregister(&lynx->misc); | |
516 | ||
517 | kfree(lynx); | |
518 | } | |
519 | ||
520 | #define RCV_BUFFER_SIZE (16 * 1024) | |
521 | ||
28646821 SR |
522 | static int __devinit |
523 | add_card(struct pci_dev *dev, const struct pci_device_id *unused) | |
524 | { | |
b5e47729 | 525 | struct pcilynx *lynx; |
28646821 | 526 | u32 p, end; |
b5e47729 | 527 | int i; |
28646821 | 528 | |
b5e47729 SR |
529 | if (pci_set_dma_mask(dev, 0xffffffff)) { |
530 | error("DMA address limits not supported " | |
531 | "for PCILynx hardware\n"); | |
532 | return -ENXIO; | |
533 | } | |
534 | if (pci_enable_device(dev)) { | |
535 | error("Failed to enable PCILynx hardware\n"); | |
536 | return -ENXIO; | |
537 | } | |
538 | pci_set_master(dev); | |
28646821 SR |
539 | |
540 | lynx = kzalloc(sizeof *lynx, GFP_KERNEL); | |
b5e47729 SR |
541 | if (lynx == NULL) { |
542 | error("Failed to allocate control structure memory\n"); | |
543 | return -ENOMEM; | |
544 | } | |
545 | lynx->pci_device = dev; | |
546 | pci_set_drvdata(dev, lynx); | |
28646821 SR |
547 | |
548 | spin_lock_init(&lynx->client_list_lock); | |
549 | INIT_LIST_HEAD(&lynx->client_list); | |
550 | ||
b5e47729 SR |
551 | lynx->registers = ioremap_nocache(pci_resource_start(dev, 0), |
552 | PCILYNX_MAX_REGISTER); | |
553 | ||
554 | lynx->rcv_start_pcl = pci_alloc_consistent(lynx->pci_device, | |
555 | sizeof(struct pcl), &lynx->rcv_start_pcl_bus); | |
556 | lynx->rcv_pcl = pci_alloc_consistent(lynx->pci_device, | |
557 | sizeof(struct pcl), &lynx->rcv_pcl_bus); | |
558 | lynx->rcv_buffer = pci_alloc_consistent(lynx->pci_device, | |
559 | RCV_BUFFER_SIZE, &lynx->rcv_buffer_bus); | |
560 | if (lynx->rcv_start_pcl == NULL || | |
28646821 | 561 | lynx->rcv_pcl == NULL || |
b5e47729 | 562 | lynx->rcv_buffer == NULL) { |
28646821 | 563 | /* FIXME: do proper error handling. */ |
b5e47729 SR |
564 | error("Failed to allocate receive buffer\n"); |
565 | return -ENOMEM; | |
566 | } | |
28646821 | 567 | lynx->rcv_start_pcl->next = lynx->rcv_pcl_bus; |
b5e47729 SR |
568 | lynx->rcv_pcl->next = PCL_NEXT_INVALID; |
569 | lynx->rcv_pcl->async_error_next = PCL_NEXT_INVALID; | |
28646821 | 570 | |
b5e47729 | 571 | lynx->rcv_pcl->buffer[0].control = |
28646821 | 572 | PCL_CMD_RCV | PCL_BIGENDIAN | 2044; |
b5e47729 | 573 | lynx->rcv_pcl->buffer[0].pointer = lynx->rcv_buffer_bus + 4; |
28646821 SR |
574 | p = lynx->rcv_buffer_bus + 2048; |
575 | end = lynx->rcv_buffer_bus + RCV_BUFFER_SIZE; | |
576 | for (i = 1; p < end; i++, p += 2048) { | |
577 | lynx->rcv_pcl->buffer[i].control = | |
578 | PCL_CMD_RCV | PCL_BIGENDIAN | 2048; | |
579 | lynx->rcv_pcl->buffer[i].pointer = p; | |
580 | } | |
581 | lynx->rcv_pcl->buffer[i - 1].control |= PCL_LAST_BUFF; | |
582 | ||
b5e47729 SR |
583 | reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET); |
584 | /* Fix buggy cards with autoboot pin not tied low: */ | |
585 | reg_write(lynx, DMA0_CHAN_CTRL, 0); | |
586 | reg_write(lynx, DMA_GLOBAL_REGISTER, 0x00 << 24); | |
28646821 SR |
587 | |
588 | #if 0 | |
b5e47729 SR |
589 | /* now, looking for PHY register set */ |
590 | if ((get_phy_reg(lynx, 2) & 0xe0) == 0xe0) { | |
591 | lynx->phyic.reg_1394a = 1; | |
592 | PRINT(KERN_INFO, lynx->id, | |
593 | "found 1394a conform PHY (using extended register set)"); | |
594 | lynx->phyic.vendor = get_phy_vendorid(lynx); | |
595 | lynx->phyic.product = get_phy_productid(lynx); | |
596 | } else { | |
597 | lynx->phyic.reg_1394a = 0; | |
598 | PRINT(KERN_INFO, lynx->id, "found old 1394 PHY"); | |
599 | } | |
28646821 SR |
600 | #endif |
601 | ||
602 | /* Setup the general receive FIFO max size. */ | |
603 | reg_write(lynx, FIFO_SIZES, 255); | |
604 | ||
b5e47729 | 605 | reg_set_bits(lynx, PCI_INT_ENABLE, PCI_INT_DMA_ALL); |
28646821 | 606 | |
b5e47729 | 607 | reg_write(lynx, LINK_INT_ENABLE, |
28646821 SR |
608 | LINK_INT_PHY_TIME_OUT | LINK_INT_PHY_REG_RCVD | |
609 | LINK_INT_PHY_BUSRESET | LINK_INT_IT_STUCK | | |
610 | LINK_INT_AT_STUCK | LINK_INT_SNTRJ | | |
611 | LINK_INT_TC_ERR | LINK_INT_GRF_OVER_FLOW | | |
612 | LINK_INT_ITF_UNDER_FLOW | LINK_INT_ATF_UNDER_FLOW); | |
613 | ||
614 | /* Disable the L flag in self ID packets. */ | |
615 | set_phy_reg(lynx, 4, 0); | |
616 | ||
617 | /* Put this baby into snoop mode */ | |
618 | reg_set_bits(lynx, LINK_CONTROL, LINK_CONTROL_SNOOP_ENABLE); | |
619 | ||
620 | run_pcl(lynx, lynx->rcv_start_pcl_bus, 0); | |
621 | ||
b5e47729 SR |
622 | if (request_irq(dev->irq, irq_handler, IRQF_SHARED, |
623 | driver_name, lynx)) { | |
624 | error("Failed to allocate shared interrupt %d\n", dev->irq); | |
625 | return -EIO; | |
626 | } | |
28646821 SR |
627 | |
628 | lynx->misc.parent = &dev->dev; | |
629 | lynx->misc.minor = MISC_DYNAMIC_MINOR; | |
630 | lynx->misc.name = "nosy"; | |
631 | lynx->misc.fops = &nosy_ops; | |
b5e47729 SR |
632 | if (misc_register(&lynx->misc)) { |
633 | error("Failed to register misc char device\n"); | |
634 | return -ENOMEM; | |
635 | } | |
28646821 SR |
636 | minors[lynx->misc.minor] = lynx; |
637 | ||
638 | notify("Initialized PCILynx IEEE1394 card, irq=%d\n", dev->irq); | |
639 | ||
b5e47729 | 640 | return 0; |
28646821 SR |
641 | } |
642 | ||
643 | static struct pci_device_id pci_table[] __devinitdata = { | |
644 | { | |
b5e47729 SR |
645 | .vendor = PCI_VENDOR_ID_TI, |
646 | .device = PCI_DEVICE_ID_TI_PCILYNX, | |
647 | .subvendor = PCI_ANY_ID, | |
648 | .subdevice = PCI_ANY_ID, | |
28646821 SR |
649 | }, |
650 | { } /* Terminating entry */ | |
651 | }; | |
652 | ||
653 | static struct pci_driver lynx_pci_driver = { | |
b5e47729 | 654 | .name = driver_name, |
28646821 SR |
655 | .id_table = pci_table, |
656 | .probe = add_card, | |
b5e47729 | 657 | .remove = remove_card, |
28646821 SR |
658 | }; |
659 | ||
b5e47729 | 660 | MODULE_AUTHOR("Kristian Hoegsberg"); |
28646821 SR |
661 | MODULE_DESCRIPTION("Snoop mode driver for TI pcilynx 1394 controllers"); |
662 | MODULE_LICENSE("GPL"); | |
663 | MODULE_DEVICE_TABLE(pci, pci_table); | |
664 | ||
665 | static int __init nosy_init(void) | |
666 | { | |
b5e47729 | 667 | return pci_register_driver(&lynx_pci_driver); |
28646821 SR |
668 | } |
669 | ||
670 | static void __exit nosy_cleanup(void) | |
671 | { | |
b5e47729 | 672 | pci_unregister_driver(&lynx_pci_driver); |
28646821 SR |
673 | |
674 | notify("Unloaded %s.\n", driver_name); | |
675 | } | |
676 | ||
28646821 SR |
677 | module_init(nosy_init); |
678 | module_exit(nosy_cleanup); |