Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ps3-vuart.c
Go to the documentation of this file.
1 /*
2  * PS3 virtual uart
3  *
4  * Copyright (C) 2006 Sony Computer Entertainment Inc.
5  * Copyright 2006 Sony Corp.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; version 2 of the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 #include <linux/kernel.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <linux/interrupt.h>
25 #include <linux/workqueue.h>
26 #include <linux/bitops.h>
27 #include <asm/ps3.h>
28 
29 #include <asm/firmware.h>
30 #include <asm/lv1call.h>
31 
32 #include "vuart.h"
33 
34 MODULE_AUTHOR("Sony Corporation");
35 MODULE_LICENSE("GPL v2");
36 MODULE_DESCRIPTION("PS3 vuart");
37 
49 enum {PORT_COUNT = 3,};
50 
55  PARAM_RX_BUF_SIZE = 3, /* read only */
56  PARAM_RX_BYTES = 4, /* read only */
57  PARAM_TX_BUF_SIZE = 5, /* read only */
58  PARAM_TX_BYTES = 6, /* read only */
59  PARAM_INTERRUPT_STATUS = 7, /* read only */
60 };
61 
66 };
67 
72 };
73 
80 
81  struct {
83  struct list_head head;
84  } tx_list;
85  struct {
87  unsigned long bytes_held;
89  struct list_head head;
90  } rx_list;
92 };
93 
94 static struct ps3_vuart_port_priv *to_port_priv(
95  struct ps3_system_bus_device *dev)
96 {
97  BUG_ON(!dev);
98  BUG_ON(!dev->driver_priv);
99  return (struct ps3_vuart_port_priv *)dev->driver_priv;
100 }
101 
109 struct ports_bmp {
112 } __attribute__((aligned(32)));
114 #define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__)
115 static void __maybe_unused _dump_ports_bmp(
116  const struct ports_bmp *bmp, const char *func, int line)
117 {
118  pr_debug("%s:%d: ports_bmp: %016llxh\n", func, line, bmp->status);
119 }
120 
121 #define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__)
122 static void __maybe_unused _dump_port_params(unsigned int port_number,
123  const char *func, int line)
124 {
125 #if defined(DEBUG)
126  static const char *strings[] = {
127  "tx_trigger ",
128  "rx_trigger ",
129  "interrupt_mask ",
130  "rx_buf_size ",
131  "rx_bytes ",
132  "tx_buf_size ",
133  "tx_bytes ",
134  "interrupt_status",
135  };
136  int result;
137  unsigned int i;
138  u64 value;
139 
140  for (i = 0; i < ARRAY_SIZE(strings); i++) {
141  result = lv1_get_virtual_uart_param(port_number, i, &value);
142 
143  if (result) {
144  pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line,
145  port_number, strings[i], ps3_result(result));
146  continue;
147  }
148  pr_debug("%s:%d: port_%u: %s = %lxh\n",
149  func, line, port_number, strings[i], value);
150  }
151 #endif
152 }
153 
155  unsigned long rx;
156  unsigned long tx;
157 };
158 
160  struct vuart_triggers *trig)
161 {
162  int result;
163  u64 size;
164  u64 val;
165  u64 tx;
166 
167  result = lv1_get_virtual_uart_param(dev->port_number,
168  PARAM_TX_TRIGGER, &tx);
169  trig->tx = tx;
170 
171  if (result) {
172  dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
173  __func__, __LINE__, ps3_result(result));
174  return result;
175  }
176 
177  result = lv1_get_virtual_uart_param(dev->port_number,
178  PARAM_RX_BUF_SIZE, &size);
179 
180  if (result) {
181  dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
182  __func__, __LINE__, ps3_result(result));
183  return result;
184  }
185 
186  result = lv1_get_virtual_uart_param(dev->port_number,
187  PARAM_RX_TRIGGER, &val);
188 
189  if (result) {
190  dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
191  __func__, __LINE__, ps3_result(result));
192  return result;
193  }
194 
195  trig->rx = size - val;
196 
197  dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__,
198  trig->tx, trig->rx);
199 
200  return result;
201 }
202 
203 int ps3_vuart_set_triggers(struct ps3_system_bus_device *dev, unsigned int tx,
204  unsigned int rx)
205 {
206  int result;
207  u64 size;
208 
209  result = lv1_set_virtual_uart_param(dev->port_number,
210  PARAM_TX_TRIGGER, tx);
211 
212  if (result) {
213  dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
214  __func__, __LINE__, ps3_result(result));
215  return result;
216  }
217 
218  result = lv1_get_virtual_uart_param(dev->port_number,
219  PARAM_RX_BUF_SIZE, &size);
220 
221  if (result) {
222  dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
223  __func__, __LINE__, ps3_result(result));
224  return result;
225  }
226 
227  result = lv1_set_virtual_uart_param(dev->port_number,
228  PARAM_RX_TRIGGER, size - rx);
229 
230  if (result) {
231  dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
232  __func__, __LINE__, ps3_result(result));
233  return result;
234  }
235 
236  dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__,
237  tx, rx);
238 
239  return result;
240 }
241 
242 static int ps3_vuart_get_rx_bytes_waiting(struct ps3_system_bus_device *dev,
243  u64 *bytes_waiting)
244 {
245  int result;
246 
247  result = lv1_get_virtual_uart_param(dev->port_number,
248  PARAM_RX_BYTES, bytes_waiting);
249 
250  if (result)
251  dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n",
252  __func__, __LINE__, ps3_result(result));
253 
254  dev_dbg(&dev->core, "%s:%d: %llxh\n", __func__, __LINE__,
255  *bytes_waiting);
256  return result;
257 }
258 
265 static int ps3_vuart_set_interrupt_mask(struct ps3_system_bus_device *dev,
266  unsigned long mask)
267 {
268  int result;
269  struct ps3_vuart_port_priv *priv = to_port_priv(dev);
270 
271  dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, mask);
272 
273  priv->interrupt_mask = mask;
274 
275  result = lv1_set_virtual_uart_param(dev->port_number,
277 
278  if (result)
279  dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n",
280  __func__, __LINE__, ps3_result(result));
281 
282  return result;
283 }
284 
285 static int ps3_vuart_get_interrupt_status(struct ps3_system_bus_device *dev,
286  unsigned long *status)
287 {
288  int result;
289  struct ps3_vuart_port_priv *priv = to_port_priv(dev);
290  u64 tmp;
291 
292  result = lv1_get_virtual_uart_param(dev->port_number,
293  PARAM_INTERRUPT_STATUS, &tmp);
294 
295  if (result)
296  dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n",
297  __func__, __LINE__, ps3_result(result));
298 
299  *status = tmp & priv->interrupt_mask;
300 
301  dev_dbg(&dev->core, "%s:%d: m %llxh, s %llxh, m&s %lxh\n",
302  __func__, __LINE__, priv->interrupt_mask, tmp, *status);
303 
304  return result;
305 }
306 
308 {
309  struct ps3_vuart_port_priv *priv = to_port_priv(dev);
310 
311  return (priv->interrupt_mask & INTERRUPT_MASK_TX) ? 0
312  : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
314 }
315 
317 {
318  struct ps3_vuart_port_priv *priv = to_port_priv(dev);
319 
320  return (priv->interrupt_mask & INTERRUPT_MASK_RX) ? 0
321  : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
323 }
324 
326 {
327  struct ps3_vuart_port_priv *priv = to_port_priv(dev);
328 
329  return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) ? 0
330  : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
332 }
333 
335 {
336  struct ps3_vuart_port_priv *priv = to_port_priv(dev);
337 
338  return (priv->interrupt_mask & INTERRUPT_MASK_TX)
339  ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
340  & ~INTERRUPT_MASK_TX) : 0;
341 }
342 
344 {
345  struct ps3_vuart_port_priv *priv = to_port_priv(dev);
346 
347  return (priv->interrupt_mask & INTERRUPT_MASK_RX)
348  ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
349  & ~INTERRUPT_MASK_RX) : 0;
350 }
351 
353 {
354  struct ps3_vuart_port_priv *priv = to_port_priv(dev);
355 
357  ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
359 }
360 
368 static int ps3_vuart_raw_write(struct ps3_system_bus_device *dev,
369  const void *buf, unsigned int bytes, u64 *bytes_written)
370 {
371  int result;
372  struct ps3_vuart_port_priv *priv = to_port_priv(dev);
373 
374  result = lv1_write_virtual_uart(dev->port_number,
375  ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written);
376 
377  if (result) {
378  dev_dbg(&dev->core, "%s:%d: lv1_write_virtual_uart failed: "
379  "%s\n", __func__, __LINE__, ps3_result(result));
380  return result;
381  }
382 
383  priv->stats.bytes_written += *bytes_written;
384 
385  dev_dbg(&dev->core, "%s:%d: wrote %llxh/%xh=>%lxh\n", __func__, __LINE__,
386  *bytes_written, bytes, priv->stats.bytes_written);
387 
388  return result;
389 }
390 
398 static int ps3_vuart_raw_read(struct ps3_system_bus_device *dev, void *buf,
399  unsigned int bytes, u64 *bytes_read)
400 {
401  int result;
402  struct ps3_vuart_port_priv *priv = to_port_priv(dev);
403 
404  dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes);
405 
406  result = lv1_read_virtual_uart(dev->port_number,
407  ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_read);
408 
409  if (result) {
410  dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n",
411  __func__, __LINE__, ps3_result(result));
412  return result;
413  }
414 
415  priv->stats.bytes_read += *bytes_read;
416 
417  dev_dbg(&dev->core, "%s:%d: read %llxh/%xh=>%lxh\n", __func__, __LINE__,
418  *bytes_read, bytes, priv->stats.bytes_read);
419 
420  return result;
421 }
422 
432  unsigned int bytes)
433 {
434  int result;
435  struct ps3_vuart_port_priv *priv = to_port_priv(dev);
436  u64 bytes_waiting;
437  void *tmp;
438 
439  result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting);
440 
441  BUG_ON(result);
442 
443  bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting;
444 
445  dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes);
446 
447  if (!bytes)
448  return;
449 
450  /* Add some extra space for recently arrived data. */
451 
452  bytes += 128;
453 
454  tmp = kmalloc(bytes, GFP_KERNEL);
455 
456  if (!tmp)
457  return;
458 
459  ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting);
460 
461  kfree(tmp);
462 
463  /* Don't include these bytes in the stats. */
464 
465  priv->stats.bytes_read -= bytes_waiting;
466 }
468 
473 struct list_buffer {
474  struct list_head link;
475  const unsigned char *head;
476  const unsigned char *tail;
477  unsigned long dbg_number;
478  unsigned char data[];
479 };
480 
491 int ps3_vuart_write(struct ps3_system_bus_device *dev, const void *buf,
492  unsigned int bytes)
493 {
494  static unsigned long dbg_number;
495  int result;
496  struct ps3_vuart_port_priv *priv = to_port_priv(dev);
497  unsigned long flags;
498  struct list_buffer *lb;
499 
500  dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
501  bytes, bytes);
502 
503  spin_lock_irqsave(&priv->tx_list.lock, flags);
504 
505  if (list_empty(&priv->tx_list.head)) {
506  u64 bytes_written;
507 
508  result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written);
509 
510  spin_unlock_irqrestore(&priv->tx_list.lock, flags);
511 
512  if (result) {
513  dev_dbg(&dev->core,
514  "%s:%d: ps3_vuart_raw_write failed\n",
515  __func__, __LINE__);
516  return result;
517  }
518 
519  if (bytes_written == bytes) {
520  dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n",
521  __func__, __LINE__, bytes);
522  return 0;
523  }
524 
525  bytes -= bytes_written;
526  buf += bytes_written;
527  } else
528  spin_unlock_irqrestore(&priv->tx_list.lock, flags);
529 
530  lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL);
531 
532  if (!lb)
533  return -ENOMEM;
534 
535  memcpy(lb->data, buf, bytes);
536  lb->head = lb->data;
537  lb->tail = lb->data + bytes;
538  lb->dbg_number = ++dbg_number;
539 
540  spin_lock_irqsave(&priv->tx_list.lock, flags);
541  list_add_tail(&lb->link, &priv->tx_list.head);
543  spin_unlock_irqrestore(&priv->tx_list.lock, flags);
544 
545  dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %xh bytes\n",
546  __func__, __LINE__, lb->dbg_number, bytes);
547 
548  return 0;
549 }
551 
560 static int ps3_vuart_queue_rx_bytes(struct ps3_system_bus_device *dev,
561  u64 *bytes_queued)
562 {
563  static unsigned long dbg_number;
564  int result;
565  struct ps3_vuart_port_priv *priv = to_port_priv(dev);
566  struct list_buffer *lb;
567  u64 bytes;
568 
569  *bytes_queued = 0;
570 
571  result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes);
572  BUG_ON(result);
573 
574  if (result)
575  return -EIO;
576 
577  if (!bytes)
578  return 0;
579 
580  /* Add some extra space for recently arrived data. */
581 
582  bytes += 128;
583 
584  lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC);
585 
586  if (!lb)
587  return -ENOMEM;
588 
589  ps3_vuart_raw_read(dev, lb->data, bytes, &bytes);
590 
591  lb->head = lb->data;
592  lb->tail = lb->data + bytes;
593  lb->dbg_number = ++dbg_number;
594 
595  list_add_tail(&lb->link, &priv->rx_list.head);
596  priv->rx_list.bytes_held += bytes;
597 
598  dev_dbg(&dev->core, "%s:%d: buf_%lu: queued %llxh bytes\n",
599  __func__, __LINE__, lb->dbg_number, bytes);
600 
601  *bytes_queued = bytes;
602 
603  return 0;
604 }
605 
615 int ps3_vuart_read(struct ps3_system_bus_device *dev, void *buf,
616  unsigned int bytes)
617 {
618  int result;
619  struct ps3_vuart_port_priv *priv = to_port_priv(dev);
620  unsigned long flags;
621  struct list_buffer *lb, *n;
622  unsigned long bytes_read;
623 
624  dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
625  bytes, bytes);
626 
627  spin_lock_irqsave(&priv->rx_list.lock, flags);
628 
629  /* Queue rx bytes here for polled reads. */
630 
631  while (priv->rx_list.bytes_held < bytes) {
632  u64 tmp;
633 
634  result = ps3_vuart_queue_rx_bytes(dev, &tmp);
635  if (result || !tmp) {
636  dev_dbg(&dev->core, "%s:%d: starved for %lxh bytes\n",
637  __func__, __LINE__,
638  bytes - priv->rx_list.bytes_held);
639  spin_unlock_irqrestore(&priv->rx_list.lock, flags);
640  return -EAGAIN;
641  }
642  }
643 
644  list_for_each_entry_safe(lb, n, &priv->rx_list.head, link) {
645  bytes_read = min((unsigned int)(lb->tail - lb->head), bytes);
646 
647  memcpy(buf, lb->head, bytes_read);
648  buf += bytes_read;
649  bytes -= bytes_read;
650  priv->rx_list.bytes_held -= bytes_read;
651 
652  if (bytes_read < lb->tail - lb->head) {
653  lb->head += bytes_read;
654  dev_dbg(&dev->core, "%s:%d: buf_%lu: dequeued %lxh "
655  "bytes\n", __func__, __LINE__, lb->dbg_number,
656  bytes_read);
657  spin_unlock_irqrestore(&priv->rx_list.lock, flags);
658  return 0;
659  }
660 
661  dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh "
662  "bytes\n", __func__, __LINE__, lb->dbg_number,
663  bytes_read);
664 
665  list_del(&lb->link);
666  kfree(lb);
667  }
668 
669  spin_unlock_irqrestore(&priv->rx_list.lock, flags);
670  return 0;
671 }
673 
678 static void ps3_vuart_work(struct work_struct *work)
679 {
680  struct ps3_system_bus_device *dev =
681  ps3_vuart_work_to_system_bus_dev(work);
682  struct ps3_vuart_port_driver *drv =
683  ps3_system_bus_dev_to_vuart_drv(dev);
684 
685  BUG_ON(!drv);
686  drv->work(dev);
687 }
688 
689 int ps3_vuart_read_async(struct ps3_system_bus_device *dev, unsigned int bytes)
690 {
691  struct ps3_vuart_port_priv *priv = to_port_priv(dev);
692  unsigned long flags;
693 
694  if (priv->rx_list.work.trigger) {
695  dev_dbg(&dev->core, "%s:%d: warning, multiple calls\n",
696  __func__, __LINE__);
697  return -EAGAIN;
698  }
699 
700  BUG_ON(!bytes);
701 
702  PREPARE_WORK(&priv->rx_list.work.work, ps3_vuart_work);
703 
704  spin_lock_irqsave(&priv->rx_list.lock, flags);
705  if (priv->rx_list.bytes_held >= bytes) {
706  dev_dbg(&dev->core, "%s:%d: schedule_work %xh bytes\n",
707  __func__, __LINE__, bytes);
708  schedule_work(&priv->rx_list.work.work);
709  spin_unlock_irqrestore(&priv->rx_list.lock, flags);
710  return 0;
711  }
712 
713  priv->rx_list.work.trigger = bytes;
714  spin_unlock_irqrestore(&priv->rx_list.lock, flags);
715 
716  dev_dbg(&dev->core, "%s:%d: waiting for %u(%xh) bytes\n", __func__,
717  __LINE__, bytes, bytes);
718 
719  return 0;
720 }
722 
724 {
725  to_port_priv(dev)->rx_list.work.trigger = 0;
726 }
728 
737 static int ps3_vuart_handle_interrupt_tx(struct ps3_system_bus_device *dev)
738 {
739  int result = 0;
740  struct ps3_vuart_port_priv *priv = to_port_priv(dev);
741  unsigned long flags;
742  struct list_buffer *lb, *n;
743  unsigned long bytes_total = 0;
744 
745  dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
746 
747  spin_lock_irqsave(&priv->tx_list.lock, flags);
748 
749  list_for_each_entry_safe(lb, n, &priv->tx_list.head, link) {
750 
751  u64 bytes_written;
752 
753  result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head,
754  &bytes_written);
755 
756  if (result) {
757  dev_dbg(&dev->core,
758  "%s:%d: ps3_vuart_raw_write failed\n",
759  __func__, __LINE__);
760  break;
761  }
762 
763  bytes_total += bytes_written;
764 
765  if (bytes_written < lb->tail - lb->head) {
766  lb->head += bytes_written;
767  dev_dbg(&dev->core,
768  "%s:%d cleared buf_%lu, %llxh bytes\n",
769  __func__, __LINE__, lb->dbg_number,
770  bytes_written);
771  goto port_full;
772  }
773 
774  dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__,
775  lb->dbg_number);
776 
777  list_del(&lb->link);
778  kfree(lb);
779  }
780 
782 port_full:
783  spin_unlock_irqrestore(&priv->tx_list.lock, flags);
784  dev_dbg(&dev->core, "%s:%d wrote %lxh bytes total\n",
785  __func__, __LINE__, bytes_total);
786  return result;
787 }
788 
797 static int ps3_vuart_handle_interrupt_rx(struct ps3_system_bus_device *dev)
798 {
799  int result;
800  struct ps3_vuart_port_priv *priv = to_port_priv(dev);
801  unsigned long flags;
802  u64 bytes;
803 
804  dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
805 
806  spin_lock_irqsave(&priv->rx_list.lock, flags);
807  result = ps3_vuart_queue_rx_bytes(dev, &bytes);
808 
809  if (result) {
810  spin_unlock_irqrestore(&priv->rx_list.lock, flags);
811  return result;
812  }
813 
814  if (priv->rx_list.work.trigger && priv->rx_list.bytes_held
815  >= priv->rx_list.work.trigger) {
816  dev_dbg(&dev->core, "%s:%d: schedule_work %lxh bytes\n",
817  __func__, __LINE__, priv->rx_list.work.trigger);
818  priv->rx_list.work.trigger = 0;
819  schedule_work(&priv->rx_list.work.work);
820  }
821 
822  spin_unlock_irqrestore(&priv->rx_list.lock, flags);
823  return result;
824 }
825 
826 static int ps3_vuart_handle_interrupt_disconnect(
827  struct ps3_system_bus_device *dev)
828 {
829  dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
830  BUG_ON("no support");
831  return -1;
832 }
833 
842 static int ps3_vuart_handle_port_interrupt(struct ps3_system_bus_device *dev)
843 {
844  int result;
845  struct ps3_vuart_port_priv *priv = to_port_priv(dev);
846  unsigned long status;
847 
848  result = ps3_vuart_get_interrupt_status(dev, &status);
849 
850  if (result)
851  return result;
852 
853  dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__,
854  status);
855 
856  if (status & INTERRUPT_MASK_DISCONNECT) {
857  priv->stats.disconnect_interrupts++;
858  result = ps3_vuart_handle_interrupt_disconnect(dev);
859  if (result)
861  }
862 
863  if (status & INTERRUPT_MASK_TX) {
864  priv->stats.tx_interrupts++;
865  result = ps3_vuart_handle_interrupt_tx(dev);
866  if (result)
868  }
869 
870  if (status & INTERRUPT_MASK_RX) {
871  priv->stats.rx_interrupts++;
872  result = ps3_vuart_handle_interrupt_rx(dev);
873  if (result)
875  }
876 
877  return 0;
878 }
879 
881  struct ports_bmp *bmp;
882  unsigned int virq;
886 } static vuart_bus_priv;
887 
896 static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private)
897 {
898  struct vuart_bus_priv *bus_priv = _private;
899 
900  BUG_ON(!bus_priv);
901 
902  while (1) {
903  unsigned int port;
904 
905  dump_ports_bmp(bus_priv->bmp);
906 
907  port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp->status);
908 
909  if (port == BITS_PER_LONG)
910  break;
911 
912  BUG_ON(port >= PORT_COUNT);
913  BUG_ON(!bus_priv->devices[port]);
914 
915  ps3_vuart_handle_port_interrupt(bus_priv->devices[port]);
916  }
917 
918  return IRQ_HANDLED;
919 }
920 
921 static int ps3_vuart_bus_interrupt_get(void)
922 {
923  int result;
924 
925  pr_debug(" -> %s:%d\n", __func__, __LINE__);
926 
928 
930 
931  if (vuart_bus_priv.use_count != 1)
932  return 0;
933 
935 
936  vuart_bus_priv.bmp = kzalloc(sizeof(struct ports_bmp), GFP_KERNEL);
937 
938  if (!vuart_bus_priv.bmp) {
939  pr_debug("%s:%d: kzalloc failed.\n", __func__, __LINE__);
940  result = -ENOMEM;
941  goto fail_bmp_malloc;
942  }
943 
946 
947  if (result) {
948  pr_debug("%s:%d: ps3_vuart_irq_setup failed (%d)\n",
949  __func__, __LINE__, result);
950  result = -EPERM;
951  goto fail_alloc_irq;
952  }
953 
954  result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler,
955  0, "vuart", &vuart_bus_priv);
956 
957  if (result) {
958  pr_debug("%s:%d: request_irq failed (%d)\n",
959  __func__, __LINE__, result);
960  goto fail_request_irq;
961  }
962 
963  pr_debug(" <- %s:%d: ok\n", __func__, __LINE__);
964  return result;
965 
966 fail_request_irq:
969 fail_alloc_irq:
972 fail_bmp_malloc:
974  pr_debug(" <- %s:%d: failed\n", __func__, __LINE__);
975  return result;
976 }
977 
978 static int ps3_vuart_bus_interrupt_put(void)
979 {
980  pr_debug(" -> %s:%d\n", __func__, __LINE__);
981 
983 
985 
986  if (vuart_bus_priv.use_count != 0)
987  return 0;
988 
990 
993 
996 
997  pr_debug(" <- %s:%d\n", __func__, __LINE__);
998  return 0;
999 }
1000 
1001 static int ps3_vuart_probe(struct ps3_system_bus_device *dev)
1002 {
1003  int result;
1004  struct ps3_vuart_port_driver *drv;
1005  struct ps3_vuart_port_priv *priv = NULL;
1006 
1007  dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
1008 
1009  drv = ps3_system_bus_dev_to_vuart_drv(dev);
1010 
1011  dev_dbg(&dev->core, "%s:%d: (%s)\n", __func__, __LINE__,
1012  drv->core.core.name);
1013 
1014  BUG_ON(!drv);
1015 
1016  if (dev->port_number >= PORT_COUNT) {
1017  BUG();
1018  return -EINVAL;
1019  }
1020 
1022 
1023  result = ps3_vuart_bus_interrupt_get();
1024 
1025  if (result)
1026  goto fail_setup_interrupt;
1027 
1028  if (vuart_bus_priv.devices[dev->port_number]) {
1029  dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__,
1030  __LINE__, dev->port_number);
1031  result = -EBUSY;
1032  goto fail_busy;
1033  }
1034 
1036 
1037  /* Setup dev->driver_priv. */
1038 
1039  dev->driver_priv = kzalloc(sizeof(struct ps3_vuart_port_priv),
1040  GFP_KERNEL);
1041 
1042  if (!dev->driver_priv) {
1043  result = -ENOMEM;
1044  goto fail_dev_malloc;
1045  }
1046 
1047  priv = to_port_priv(dev);
1048 
1049  INIT_LIST_HEAD(&priv->tx_list.head);
1050  spin_lock_init(&priv->tx_list.lock);
1051 
1052  INIT_LIST_HEAD(&priv->rx_list.head);
1053  spin_lock_init(&priv->rx_list.lock);
1054 
1055  INIT_WORK(&priv->rx_list.work.work, NULL);
1056  priv->rx_list.work.trigger = 0;
1057  priv->rx_list.work.dev = dev;
1058 
1059  /* clear stale pending interrupts */
1060 
1061  ps3_vuart_clear_rx_bytes(dev, 0);
1062 
1063  ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX);
1064 
1065  ps3_vuart_set_triggers(dev, 1, 1);
1066 
1067  if (drv->probe)
1068  result = drv->probe(dev);
1069  else {
1070  result = 0;
1071  dev_info(&dev->core, "%s:%d: no probe method\n", __func__,
1072  __LINE__);
1073  }
1074 
1075  if (result) {
1076  dev_dbg(&dev->core, "%s:%d: drv->probe failed\n",
1077  __func__, __LINE__);
1078  goto fail_probe;
1079  }
1080 
1082 
1083  return result;
1084 
1085 fail_probe:
1086  ps3_vuart_set_interrupt_mask(dev, 0);
1087  kfree(dev->driver_priv);
1088  dev->driver_priv = NULL;
1089 fail_dev_malloc:
1091 fail_busy:
1092  ps3_vuart_bus_interrupt_put();
1093 fail_setup_interrupt:
1095  dev_dbg(&dev->core, "%s:%d: failed\n", __func__, __LINE__);
1096  return result;
1097 }
1098 
1108 static int ps3_vuart_cleanup(struct ps3_system_bus_device *dev)
1109 {
1110  dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
1111 
1113  ps3_vuart_set_interrupt_mask(dev, 0);
1114  ps3_vuart_bus_interrupt_put();
1115  return 0;
1116 }
1117 
1126 static int ps3_vuart_remove(struct ps3_system_bus_device *dev)
1127 {
1128  struct ps3_vuart_port_priv *priv = to_port_priv(dev);
1129  struct ps3_vuart_port_driver *drv;
1130 
1131  BUG_ON(!dev);
1132 
1134 
1135  dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
1136  dev->match_id);
1137 
1138  if (!dev->core.driver) {
1139  dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
1140  __LINE__);
1142  return 0;
1143  }
1144 
1145  drv = ps3_system_bus_dev_to_vuart_drv(dev);
1146 
1147  BUG_ON(!drv);
1148 
1149  if (drv->remove) {
1150  drv->remove(dev);
1151  } else {
1152  dev_dbg(&dev->core, "%s:%d: no remove method\n", __func__,
1153  __LINE__);
1154  BUG();
1155  }
1156 
1157  ps3_vuart_cleanup(dev);
1158 
1160  kfree(priv);
1161  priv = NULL;
1162 
1163  dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
1165  return 0;
1166 }
1167 
1178 static int ps3_vuart_shutdown(struct ps3_system_bus_device *dev)
1179 {
1180  struct ps3_vuart_port_driver *drv;
1181 
1182  BUG_ON(!dev);
1183 
1185 
1186  dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
1187  dev->match_id);
1188 
1189  if (!dev->core.driver) {
1190  dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
1191  __LINE__);
1193  return 0;
1194  }
1195 
1196  drv = ps3_system_bus_dev_to_vuart_drv(dev);
1197 
1198  BUG_ON(!drv);
1199 
1200  if (drv->shutdown)
1201  drv->shutdown(dev);
1202  else if (drv->remove) {
1203  dev_dbg(&dev->core, "%s:%d: no shutdown, calling remove\n",
1204  __func__, __LINE__);
1205  drv->remove(dev);
1206  } else {
1207  dev_dbg(&dev->core, "%s:%d: no shutdown method\n", __func__,
1208  __LINE__);
1209  BUG();
1210  }
1211 
1212  ps3_vuart_cleanup(dev);
1213 
1214  dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
1215 
1217  return 0;
1218 }
1219 
1220 static int __init ps3_vuart_bus_init(void)
1221 {
1222  pr_debug("%s:%d:\n", __func__, __LINE__);
1223 
1224  if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
1225  return -ENODEV;
1226 
1228 
1229  return 0;
1230 }
1231 
1232 static void __exit ps3_vuart_bus_exit(void)
1233 {
1234  pr_debug("%s:%d:\n", __func__, __LINE__);
1235 }
1236 
1237 core_initcall(ps3_vuart_bus_init);
1238 module_exit(ps3_vuart_bus_exit);
1239 
1245 {
1246  int result;
1247 
1248  pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name);
1249 
1250  BUG_ON(!drv->core.match_id);
1251  BUG_ON(!drv->core.core.name);
1252 
1253  drv->core.probe = ps3_vuart_probe;
1254  drv->core.remove = ps3_vuart_remove;
1255  drv->core.shutdown = ps3_vuart_shutdown;
1256 
1257  result = ps3_system_bus_driver_register(&drv->core);
1258  return result;
1259 }
1261 
1267 {
1268  pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name);
1270 }