vuart.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/interrupt.h>
  23. #include <asm/ps3.h>
  24. #include <asm/firmware.h>
  25. #include <asm/lv1call.h>
  26. #include <asm/bitops.h>
  27. #include "vuart.h"
  28. MODULE_AUTHOR("Sony Corporation");
  29. MODULE_LICENSE("GPL v2");
  30. MODULE_DESCRIPTION("PS3 vuart");
  31. /**
  32. * vuart - An inter-partition data link service.
  33. * port 0: PS3 AV Settings.
  34. * port 2: PS3 System Manager.
  35. *
  36. * The vuart provides a bi-directional byte stream data link between logical
  37. * partitions. Its primary role is as a communications link between the guest
  38. * OS and the system policy module. The current HV does not support any
  39. * connections other than those listed.
  40. */
  41. enum {PORT_COUNT = 3,};
  42. enum vuart_param {
  43. PARAM_TX_TRIGGER = 0,
  44. PARAM_RX_TRIGGER = 1,
  45. PARAM_INTERRUPT_MASK = 2,
  46. PARAM_RX_BUF_SIZE = 3, /* read only */
  47. PARAM_RX_BYTES = 4, /* read only */
  48. PARAM_TX_BUF_SIZE = 5, /* read only */
  49. PARAM_TX_BYTES = 6, /* read only */
  50. PARAM_INTERRUPT_STATUS = 7, /* read only */
  51. };
  52. enum vuart_interrupt_bit {
  53. INTERRUPT_BIT_TX = 0,
  54. INTERRUPT_BIT_RX = 1,
  55. INTERRUPT_BIT_DISCONNECT = 2,
  56. };
  57. enum vuart_interrupt_mask {
  58. INTERRUPT_MASK_TX = 1,
  59. INTERRUPT_MASK_RX = 2,
  60. INTERRUPT_MASK_DISCONNECT = 4,
  61. };
  62. /**
  63. * struct ports_bmp - bitmap indicating ports needing service.
  64. *
  65. * A 256 bit read only bitmap indicating ports needing service. Do not write
  66. * to these bits. Must not cross a page boundary.
  67. */
  68. struct ports_bmp {
  69. u64 status;
  70. u64 unused[3];
  71. } __attribute__ ((aligned (32)));
  72. /* redefine dev_dbg to do a syntax check */
  73. #if !defined(DEBUG)
  74. #undef dev_dbg
  75. static inline int __attribute__ ((format (printf, 2, 3))) dev_dbg(
  76. const struct device *_dev, const char *fmt, ...) {return 0;}
  77. #endif
  78. #define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__)
  79. static void __attribute__ ((unused)) _dump_ports_bmp(
  80. const struct ports_bmp* bmp, const char* func, int line)
  81. {
  82. pr_debug("%s:%d: ports_bmp: %016lxh\n", func, line, bmp->status);
  83. }
  84. static int ps3_vuart_match_id_to_port(enum ps3_match_id match_id,
  85. unsigned int *port_number)
  86. {
  87. switch(match_id) {
  88. case PS3_MATCH_ID_AV_SETTINGS:
  89. *port_number = 0;
  90. return 0;
  91. case PS3_MATCH_ID_SYSTEM_MANAGER:
  92. *port_number = 2;
  93. return 0;
  94. default:
  95. WARN_ON(1);
  96. *port_number = UINT_MAX;
  97. return -EINVAL;
  98. };
  99. }
  100. #define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__)
  101. static void __attribute__ ((unused)) _dump_port_params(unsigned int port_number,
  102. const char* func, int line)
  103. {
  104. #if defined(DEBUG)
  105. static const char *strings[] = {
  106. "tx_trigger ",
  107. "rx_trigger ",
  108. "interrupt_mask ",
  109. "rx_buf_size ",
  110. "rx_bytes ",
  111. "tx_buf_size ",
  112. "tx_bytes ",
  113. "interrupt_status",
  114. };
  115. int result;
  116. unsigned int i;
  117. u64 value;
  118. for (i = 0; i < ARRAY_SIZE(strings); i++) {
  119. result = lv1_get_virtual_uart_param(port_number, i, &value);
  120. if (result) {
  121. pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line,
  122. port_number, strings[i], ps3_result(result));
  123. continue;
  124. }
  125. pr_debug("%s:%d: port_%u: %s = %lxh\n",
  126. func, line, port_number, strings[i], value);
  127. }
  128. #endif
  129. }
  130. struct vuart_triggers {
  131. unsigned long rx;
  132. unsigned long tx;
  133. };
  134. int ps3_vuart_get_triggers(struct ps3_vuart_port_device *dev,
  135. struct vuart_triggers *trig)
  136. {
  137. int result;
  138. unsigned long size;
  139. unsigned long val;
  140. result = lv1_get_virtual_uart_param(dev->priv->port_number,
  141. PARAM_TX_TRIGGER, &trig->tx);
  142. if (result) {
  143. dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
  144. __func__, __LINE__, ps3_result(result));
  145. return result;
  146. }
  147. result = lv1_get_virtual_uart_param(dev->priv->port_number,
  148. PARAM_RX_BUF_SIZE, &size);
  149. if (result) {
  150. dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
  151. __func__, __LINE__, ps3_result(result));
  152. return result;
  153. }
  154. result = lv1_get_virtual_uart_param(dev->priv->port_number,
  155. PARAM_RX_TRIGGER, &val);
  156. if (result) {
  157. dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
  158. __func__, __LINE__, ps3_result(result));
  159. return result;
  160. }
  161. trig->rx = size - val;
  162. dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__,
  163. trig->tx, trig->rx);
  164. return result;
  165. }
  166. int ps3_vuart_set_triggers(struct ps3_vuart_port_device *dev, unsigned int tx,
  167. unsigned int rx)
  168. {
  169. int result;
  170. unsigned long size;
  171. result = lv1_set_virtual_uart_param(dev->priv->port_number,
  172. PARAM_TX_TRIGGER, tx);
  173. if (result) {
  174. dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
  175. __func__, __LINE__, ps3_result(result));
  176. return result;
  177. }
  178. result = lv1_get_virtual_uart_param(dev->priv->port_number,
  179. PARAM_RX_BUF_SIZE, &size);
  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. result = lv1_set_virtual_uart_param(dev->priv->port_number,
  186. PARAM_RX_TRIGGER, size - rx);
  187. if (result) {
  188. dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
  189. __func__, __LINE__, ps3_result(result));
  190. return result;
  191. }
  192. dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__,
  193. tx, rx);
  194. return result;
  195. }
  196. static int ps3_vuart_get_rx_bytes_waiting(struct ps3_vuart_port_device *dev,
  197. u64 *bytes_waiting)
  198. {
  199. int result = lv1_get_virtual_uart_param(dev->priv->port_number,
  200. PARAM_RX_BYTES, bytes_waiting);
  201. if (result)
  202. dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n",
  203. __func__, __LINE__, ps3_result(result));
  204. dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__,
  205. *bytes_waiting);
  206. return result;
  207. }
  208. static int ps3_vuart_set_interrupt_mask(struct ps3_vuart_port_device *dev,
  209. unsigned long mask)
  210. {
  211. int result;
  212. dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, mask);
  213. dev->priv->interrupt_mask = mask;
  214. result = lv1_set_virtual_uart_param(dev->priv->port_number,
  215. PARAM_INTERRUPT_MASK, dev->priv->interrupt_mask);
  216. if (result)
  217. dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n",
  218. __func__, __LINE__, ps3_result(result));
  219. return result;
  220. }
  221. static int ps3_vuart_get_interrupt_status(struct ps3_vuart_port_device *dev,
  222. unsigned long *status)
  223. {
  224. u64 tmp;
  225. int result = lv1_get_virtual_uart_param(dev->priv->port_number,
  226. PARAM_INTERRUPT_STATUS, &tmp);
  227. if (result)
  228. dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n",
  229. __func__, __LINE__, ps3_result(result));
  230. *status = tmp & dev->priv->interrupt_mask;
  231. dev_dbg(&dev->core, "%s:%d: m %lxh, s %lxh, m&s %lxh\n",
  232. __func__, __LINE__, dev->priv->interrupt_mask, tmp, *status);
  233. return result;
  234. }
  235. int ps3_vuart_enable_interrupt_tx(struct ps3_vuart_port_device *dev)
  236. {
  237. return (dev->priv->interrupt_mask & INTERRUPT_MASK_TX) ? 0
  238. : ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
  239. | INTERRUPT_MASK_TX);
  240. }
  241. int ps3_vuart_enable_interrupt_rx(struct ps3_vuart_port_device *dev)
  242. {
  243. return (dev->priv->interrupt_mask & INTERRUPT_MASK_RX) ? 0
  244. : ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
  245. | INTERRUPT_MASK_RX);
  246. }
  247. int ps3_vuart_enable_interrupt_disconnect(struct ps3_vuart_port_device *dev)
  248. {
  249. return (dev->priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) ? 0
  250. : ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
  251. | INTERRUPT_MASK_DISCONNECT);
  252. }
  253. int ps3_vuart_disable_interrupt_tx(struct ps3_vuart_port_device *dev)
  254. {
  255. return (dev->priv->interrupt_mask & INTERRUPT_MASK_TX)
  256. ? ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
  257. & ~INTERRUPT_MASK_TX) : 0;
  258. }
  259. int ps3_vuart_disable_interrupt_rx(struct ps3_vuart_port_device *dev)
  260. {
  261. return (dev->priv->interrupt_mask & INTERRUPT_MASK_RX)
  262. ? ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
  263. & ~INTERRUPT_MASK_RX) : 0;
  264. }
  265. int ps3_vuart_disable_interrupt_disconnect(struct ps3_vuart_port_device *dev)
  266. {
  267. return (dev->priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT)
  268. ? ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
  269. & ~INTERRUPT_MASK_DISCONNECT) : 0;
  270. }
  271. /**
  272. * ps3_vuart_raw_write - Low level write helper.
  273. *
  274. * Do not call ps3_vuart_raw_write directly, use ps3_vuart_write.
  275. */
  276. static int ps3_vuart_raw_write(struct ps3_vuart_port_device *dev,
  277. const void* buf, unsigned int bytes, unsigned long *bytes_written)
  278. {
  279. int result;
  280. result = lv1_write_virtual_uart(dev->priv->port_number,
  281. ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written);
  282. if (result) {
  283. dev_dbg(&dev->core, "%s:%d: lv1_write_virtual_uart failed: "
  284. "%s\n", __func__, __LINE__, ps3_result(result));
  285. return result;
  286. }
  287. dev->priv->stats.bytes_written += *bytes_written;
  288. dev_dbg(&dev->core, "%s:%d: wrote %lxh/%xh=>%lxh\n", __func__, __LINE__,
  289. *bytes_written, bytes, dev->priv->stats.bytes_written);
  290. return result;
  291. }
  292. /**
  293. * ps3_vuart_raw_read - Low level read helper.
  294. *
  295. * Do not call ps3_vuart_raw_read directly, use ps3_vuart_read.
  296. */
  297. static int ps3_vuart_raw_read(struct ps3_vuart_port_device *dev, void* buf,
  298. unsigned int bytes, unsigned long *bytes_read)
  299. {
  300. int result;
  301. dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes);
  302. result = lv1_read_virtual_uart(dev->priv->port_number,
  303. ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_read);
  304. if (result) {
  305. dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n",
  306. __func__, __LINE__, ps3_result(result));
  307. return result;
  308. }
  309. dev->priv->stats.bytes_read += *bytes_read;
  310. dev_dbg(&dev->core, "%s:%d: read %lxh/%xh=>%lxh\n", __func__, __LINE__,
  311. *bytes_read, bytes, dev->priv->stats.bytes_read);
  312. return result;
  313. }
  314. /**
  315. * ps3_vuart_clear_rx_bytes - Discard bytes received.
  316. * @bytes: Max byte count to discard, zero = all pending.
  317. *
  318. * Used to clear pending rx interrupt source. Will not block.
  319. */
  320. void ps3_vuart_clear_rx_bytes(struct ps3_vuart_port_device *dev,
  321. unsigned int bytes)
  322. {
  323. int result;
  324. u64 bytes_waiting;
  325. void* tmp;
  326. result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting);
  327. BUG_ON(result);
  328. bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting;
  329. dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes);
  330. if (!bytes)
  331. return;
  332. /* Add some extra space for recently arrived data. */
  333. bytes += 128;
  334. tmp = kmalloc(bytes, GFP_KERNEL);
  335. if (!tmp)
  336. return;
  337. ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting);
  338. kfree(tmp);
  339. /* Don't include these bytes in the stats. */
  340. dev->priv->stats.bytes_read -= bytes_waiting;
  341. }
  342. /**
  343. * struct list_buffer - An element for a port device fifo buffer list.
  344. */
  345. struct list_buffer {
  346. struct list_head link;
  347. const unsigned char *head;
  348. const unsigned char *tail;
  349. unsigned long dbg_number;
  350. unsigned char data[];
  351. };
  352. /**
  353. * ps3_vuart_write - the entry point for writing data to a port
  354. *
  355. * If the port is idle on entry as much of the incoming data is written to
  356. * the port as the port will accept. Otherwise a list buffer is created
  357. * and any remaning incoming data is copied to that buffer. The buffer is
  358. * then enqueued for transmision via the transmit interrupt.
  359. */
  360. int ps3_vuart_write(struct ps3_vuart_port_device *dev, const void* buf,
  361. unsigned int bytes)
  362. {
  363. static unsigned long dbg_number;
  364. int result;
  365. unsigned long flags;
  366. struct list_buffer *lb;
  367. dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
  368. bytes, bytes);
  369. spin_lock_irqsave(&dev->priv->tx_list.lock, flags);
  370. if (list_empty(&dev->priv->tx_list.head)) {
  371. unsigned long bytes_written;
  372. result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written);
  373. spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
  374. if (result) {
  375. dev_dbg(&dev->core,
  376. "%s:%d: ps3_vuart_raw_write failed\n",
  377. __func__, __LINE__);
  378. return result;
  379. }
  380. if (bytes_written == bytes) {
  381. dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n",
  382. __func__, __LINE__, bytes);
  383. return 0;
  384. }
  385. bytes -= bytes_written;
  386. buf += bytes_written;
  387. } else
  388. spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
  389. lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL);
  390. if (!lb) {
  391. return -ENOMEM;
  392. }
  393. memcpy(lb->data, buf, bytes);
  394. lb->head = lb->data;
  395. lb->tail = lb->data + bytes;
  396. lb->dbg_number = ++dbg_number;
  397. spin_lock_irqsave(&dev->priv->tx_list.lock, flags);
  398. list_add_tail(&lb->link, &dev->priv->tx_list.head);
  399. ps3_vuart_enable_interrupt_tx(dev);
  400. spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
  401. dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %xh bytes\n",
  402. __func__, __LINE__, lb->dbg_number, bytes);
  403. return 0;
  404. }
  405. /**
  406. * ps3_vuart_read - the entry point for reading data from a port
  407. *
  408. * If enough bytes to satisfy the request are held in the buffer list those
  409. * bytes are dequeued and copied to the caller's buffer. Emptied list buffers
  410. * are retiered. If the request cannot be statified by bytes held in the list
  411. * buffers -EAGAIN is returned.
  412. */
  413. int ps3_vuart_read(struct ps3_vuart_port_device *dev, void* buf,
  414. unsigned int bytes)
  415. {
  416. unsigned long flags;
  417. struct list_buffer *lb, *n;
  418. unsigned long bytes_read;
  419. dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
  420. bytes, bytes);
  421. spin_lock_irqsave(&dev->priv->rx_list.lock, flags);
  422. if (dev->priv->rx_list.bytes_held < bytes) {
  423. spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
  424. dev_dbg(&dev->core, "%s:%d: starved for %lxh bytes\n",
  425. __func__, __LINE__,
  426. bytes - dev->priv->rx_list.bytes_held);
  427. return -EAGAIN;
  428. }
  429. list_for_each_entry_safe(lb, n, &dev->priv->rx_list.head, link) {
  430. bytes_read = min((unsigned int)(lb->tail - lb->head), bytes);
  431. memcpy(buf, lb->head, bytes_read);
  432. buf += bytes_read;
  433. bytes -= bytes_read;
  434. dev->priv->rx_list.bytes_held -= bytes_read;
  435. if (bytes_read < lb->tail - lb->head) {
  436. lb->head += bytes_read;
  437. dev_dbg(&dev->core, "%s:%d: buf_%lu: dequeued %lxh "
  438. "bytes\n", __func__, __LINE__, lb->dbg_number,
  439. bytes_read);
  440. spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
  441. return 0;
  442. }
  443. dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh "
  444. "bytes\n", __func__, __LINE__, lb->dbg_number,
  445. bytes_read);
  446. list_del(&lb->link);
  447. kfree(lb);
  448. }
  449. spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
  450. return 0;
  451. }
  452. /**
  453. * ps3_vuart_handle_interrupt_tx - third stage transmit interrupt handler
  454. *
  455. * Services the transmit interrupt for the port. Writes as much data from the
  456. * buffer list as the port will accept. Retires any emptied list buffers and
  457. * adjusts the final list buffer state for a partial write.
  458. */
  459. static int ps3_vuart_handle_interrupt_tx(struct ps3_vuart_port_device *dev)
  460. {
  461. int result = 0;
  462. unsigned long flags;
  463. struct list_buffer *lb, *n;
  464. unsigned long bytes_total = 0;
  465. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  466. spin_lock_irqsave(&dev->priv->tx_list.lock, flags);
  467. list_for_each_entry_safe(lb, n, &dev->priv->tx_list.head, link) {
  468. unsigned long bytes_written;
  469. result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head,
  470. &bytes_written);
  471. if (result) {
  472. dev_dbg(&dev->core,
  473. "%s:%d: ps3_vuart_raw_write failed\n",
  474. __func__, __LINE__);
  475. break;
  476. }
  477. bytes_total += bytes_written;
  478. if (bytes_written < lb->tail - lb->head) {
  479. lb->head += bytes_written;
  480. dev_dbg(&dev->core,
  481. "%s:%d cleared buf_%lu, %lxh bytes\n",
  482. __func__, __LINE__, lb->dbg_number,
  483. bytes_written);
  484. goto port_full;
  485. }
  486. dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__,
  487. lb->dbg_number);
  488. list_del(&lb->link);
  489. kfree(lb);
  490. }
  491. ps3_vuart_disable_interrupt_tx(dev);
  492. port_full:
  493. spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
  494. dev_dbg(&dev->core, "%s:%d wrote %lxh bytes total\n",
  495. __func__, __LINE__, bytes_total);
  496. return result;
  497. }
  498. /**
  499. * ps3_vuart_handle_interrupt_rx - third stage receive interrupt handler
  500. *
  501. * Services the receive interrupt for the port. Creates a list buffer and
  502. * copies all waiting port data to that buffer and enqueues the buffer in the
  503. * buffer list. Buffer list data is dequeued via ps3_vuart_read.
  504. */
  505. static int ps3_vuart_handle_interrupt_rx(struct ps3_vuart_port_device *dev)
  506. {
  507. static unsigned long dbg_number;
  508. int result = 0;
  509. unsigned long flags;
  510. struct list_buffer *lb;
  511. unsigned long bytes;
  512. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  513. result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes);
  514. if (result)
  515. return -EIO;
  516. BUG_ON(!bytes);
  517. /* Add some extra space for recently arrived data. */
  518. bytes += 128;
  519. lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC);
  520. if (!lb)
  521. return -ENOMEM;
  522. ps3_vuart_raw_read(dev, lb->data, bytes, &bytes);
  523. lb->head = lb->data;
  524. lb->tail = lb->data + bytes;
  525. lb->dbg_number = ++dbg_number;
  526. spin_lock_irqsave(&dev->priv->rx_list.lock, flags);
  527. list_add_tail(&lb->link, &dev->priv->rx_list.head);
  528. dev->priv->rx_list.bytes_held += bytes;
  529. spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
  530. dev_dbg(&dev->core, "%s:%d: buf_%lu: queued %lxh bytes\n",
  531. __func__, __LINE__, lb->dbg_number, bytes);
  532. return 0;
  533. }
  534. static int ps3_vuart_handle_interrupt_disconnect(
  535. struct ps3_vuart_port_device *dev)
  536. {
  537. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  538. BUG_ON("no support");
  539. return -1;
  540. }
  541. /**
  542. * ps3_vuart_handle_port_interrupt - second stage interrupt handler
  543. *
  544. * Services any pending interrupt types for the port. Passes control to the
  545. * third stage type specific interrupt handler. Returns control to the first
  546. * stage handler after one iteration.
  547. */
  548. static int ps3_vuart_handle_port_interrupt(struct ps3_vuart_port_device *dev)
  549. {
  550. int result;
  551. unsigned long status;
  552. result = ps3_vuart_get_interrupt_status(dev, &status);
  553. if (result)
  554. return result;
  555. dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__,
  556. status);
  557. if (status & INTERRUPT_MASK_DISCONNECT) {
  558. dev->priv->stats.disconnect_interrupts++;
  559. result = ps3_vuart_handle_interrupt_disconnect(dev);
  560. if (result)
  561. ps3_vuart_disable_interrupt_disconnect(dev);
  562. }
  563. if (status & INTERRUPT_MASK_TX) {
  564. dev->priv->stats.tx_interrupts++;
  565. result = ps3_vuart_handle_interrupt_tx(dev);
  566. if (result)
  567. ps3_vuart_disable_interrupt_tx(dev);
  568. }
  569. if (status & INTERRUPT_MASK_RX) {
  570. dev->priv->stats.rx_interrupts++;
  571. result = ps3_vuart_handle_interrupt_rx(dev);
  572. if (result)
  573. ps3_vuart_disable_interrupt_rx(dev);
  574. }
  575. return 0;
  576. }
  577. struct vuart_bus_priv {
  578. const struct ports_bmp bmp;
  579. unsigned int virq;
  580. struct semaphore probe_mutex;
  581. int use_count;
  582. struct ps3_vuart_port_device *devices[PORT_COUNT];
  583. } static vuart_bus_priv;
  584. /**
  585. * ps3_vuart_irq_handler - first stage interrupt handler
  586. *
  587. * Loops finding any interrupting port and its associated instance data.
  588. * Passes control to the second stage port specific interrupt handler. Loops
  589. * until all outstanding interrupts are serviced.
  590. */
  591. static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private)
  592. {
  593. struct vuart_bus_priv *bus_priv;
  594. BUG_ON(!_private);
  595. bus_priv = (struct vuart_bus_priv *)_private;
  596. while (1) {
  597. unsigned int port;
  598. dump_ports_bmp(&bus_priv->bmp);
  599. port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp.status);
  600. if (port == BITS_PER_LONG)
  601. break;
  602. BUG_ON(port >= PORT_COUNT);
  603. BUG_ON(!bus_priv->devices[port]);
  604. ps3_vuart_handle_port_interrupt(bus_priv->devices[port]);
  605. }
  606. return IRQ_HANDLED;
  607. }
  608. static int ps3_vuart_match(struct device *_dev, struct device_driver *_drv)
  609. {
  610. int result;
  611. struct ps3_vuart_port_driver *drv = to_ps3_vuart_port_driver(_drv);
  612. struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
  613. result = dev->match_id == drv->match_id;
  614. dev_info(&dev->core, "%s:%d: dev=%u(%s), drv=%u(%s): %s\n", __func__,
  615. __LINE__, dev->match_id, dev->core.bus_id, drv->match_id,
  616. drv->core.name, (result ? "match" : "miss"));
  617. return result;
  618. }
  619. static int ps3_vuart_probe(struct device *_dev)
  620. {
  621. int result;
  622. unsigned int port_number;
  623. struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
  624. struct ps3_vuart_port_driver *drv =
  625. to_ps3_vuart_port_driver(_dev->driver);
  626. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  627. BUG_ON(!drv);
  628. down(&vuart_bus_priv.probe_mutex);
  629. /* Setup vuart_bus_priv.devices[]. */
  630. result = ps3_vuart_match_id_to_port(dev->match_id,
  631. &port_number);
  632. if (result) {
  633. dev_dbg(&dev->core, "%s:%d: unknown match_id (%d)\n",
  634. __func__, __LINE__, dev->match_id);
  635. result = -EINVAL;
  636. goto fail_match;
  637. }
  638. if (vuart_bus_priv.devices[port_number]) {
  639. dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__,
  640. __LINE__, port_number);
  641. result = -EBUSY;
  642. goto fail_match;
  643. }
  644. vuart_bus_priv.devices[port_number] = dev;
  645. /* Setup dev->priv. */
  646. dev->priv = kzalloc(sizeof(struct ps3_vuart_port_priv), GFP_KERNEL);
  647. if (!dev->priv) {
  648. result = -ENOMEM;
  649. goto fail_alloc;
  650. }
  651. dev->priv->port_number = port_number;
  652. INIT_LIST_HEAD(&dev->priv->tx_list.head);
  653. spin_lock_init(&dev->priv->tx_list.lock);
  654. INIT_LIST_HEAD(&dev->priv->rx_list.head);
  655. spin_lock_init(&dev->priv->rx_list.lock);
  656. if (++vuart_bus_priv.use_count == 1) {
  657. result = ps3_alloc_vuart_irq(PS3_BINDING_CPU_ANY,
  658. (void*)&vuart_bus_priv.bmp.status, &vuart_bus_priv.virq);
  659. if (result) {
  660. dev_dbg(&dev->core,
  661. "%s:%d: ps3_alloc_vuart_irq failed (%d)\n",
  662. __func__, __LINE__, result);
  663. result = -EPERM;
  664. goto fail_alloc_irq;
  665. }
  666. result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler,
  667. IRQF_DISABLED, "vuart", &vuart_bus_priv);
  668. if (result) {
  669. dev_info(&dev->core, "%s:%d: request_irq failed (%d)\n",
  670. __func__, __LINE__, result);
  671. goto fail_request_irq;
  672. }
  673. }
  674. /* clear stale pending interrupts */
  675. ps3_vuart_clear_rx_bytes(dev, 0);
  676. ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX);
  677. ps3_vuart_set_triggers(dev, 1, 1);
  678. if (drv->probe)
  679. result = drv->probe(dev);
  680. else {
  681. result = 0;
  682. dev_info(&dev->core, "%s:%d: no probe method\n", __func__,
  683. __LINE__);
  684. }
  685. if (result) {
  686. dev_dbg(&dev->core, "%s:%d: drv->probe failed\n",
  687. __func__, __LINE__);
  688. down(&vuart_bus_priv.probe_mutex);
  689. goto fail_probe;
  690. }
  691. up(&vuart_bus_priv.probe_mutex);
  692. return result;
  693. fail_probe:
  694. ps3_vuart_set_interrupt_mask(dev, 0);
  695. fail_request_irq:
  696. ps3_free_vuart_irq(vuart_bus_priv.virq);
  697. vuart_bus_priv.virq = NO_IRQ;
  698. fail_alloc_irq:
  699. --vuart_bus_priv.use_count;
  700. kfree(dev->priv);
  701. dev->priv = NULL;
  702. fail_alloc:
  703. vuart_bus_priv.devices[port_number] = 0;
  704. fail_match:
  705. up(&vuart_bus_priv.probe_mutex);
  706. dev_dbg(&dev->core, "%s:%d failed\n", __func__, __LINE__);
  707. return result;
  708. }
  709. static int ps3_vuart_remove(struct device *_dev)
  710. {
  711. struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
  712. struct ps3_vuart_port_driver *drv =
  713. to_ps3_vuart_port_driver(_dev->driver);
  714. down(&vuart_bus_priv.probe_mutex);
  715. dev_dbg(&dev->core, "%s:%d: %s\n", __func__, __LINE__,
  716. dev->core.bus_id);
  717. BUG_ON(vuart_bus_priv.use_count < 1);
  718. if (drv->remove)
  719. drv->remove(dev);
  720. else
  721. dev_dbg(&dev->core, "%s:%d: %s no remove method\n", __func__,
  722. __LINE__, dev->core.bus_id);
  723. vuart_bus_priv.devices[dev->priv->port_number] = 0;
  724. if (--vuart_bus_priv.use_count == 0) {
  725. BUG();
  726. free_irq(vuart_bus_priv.virq, &vuart_bus_priv);
  727. ps3_free_vuart_irq(vuart_bus_priv.virq);
  728. vuart_bus_priv.virq = NO_IRQ;
  729. }
  730. kfree(dev->priv);
  731. dev->priv = NULL;
  732. up(&vuart_bus_priv.probe_mutex);
  733. return 0;
  734. }
  735. static void ps3_vuart_shutdown(struct device *_dev)
  736. {
  737. struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
  738. struct ps3_vuart_port_driver *drv =
  739. to_ps3_vuart_port_driver(_dev->driver);
  740. dev_dbg(&dev->core, "%s:%d: %s\n", __func__, __LINE__,
  741. dev->core.bus_id);
  742. if (drv->shutdown)
  743. drv->shutdown(dev);
  744. else
  745. dev_dbg(&dev->core, "%s:%d: %s no shutdown method\n", __func__,
  746. __LINE__, dev->core.bus_id);
  747. }
  748. /**
  749. * ps3_vuart_bus - The vuart bus instance.
  750. *
  751. * The vuart is managed as a bus that port devices connect to.
  752. */
  753. struct bus_type ps3_vuart_bus = {
  754. .name = "ps3_vuart",
  755. .match = ps3_vuart_match,
  756. .probe = ps3_vuart_probe,
  757. .remove = ps3_vuart_remove,
  758. .shutdown = ps3_vuart_shutdown,
  759. };
  760. int __init ps3_vuart_bus_init(void)
  761. {
  762. int result;
  763. pr_debug("%s:%d:\n", __func__, __LINE__);
  764. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  765. return 0;
  766. init_MUTEX(&vuart_bus_priv.probe_mutex);
  767. result = bus_register(&ps3_vuart_bus);
  768. BUG_ON(result);
  769. return result;
  770. }
  771. void __exit ps3_vuart_bus_exit(void)
  772. {
  773. pr_debug("%s:%d:\n", __func__, __LINE__);
  774. bus_unregister(&ps3_vuart_bus);
  775. }
  776. core_initcall(ps3_vuart_bus_init);
  777. module_exit(ps3_vuart_bus_exit);
  778. /**
  779. * ps3_vuart_port_release_device - Remove a vuart port device.
  780. */
  781. static void ps3_vuart_port_release_device(struct device *_dev)
  782. {
  783. #if defined(DEBUG)
  784. struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
  785. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  786. BUG_ON(dev->priv && "forgot to free");
  787. memset(&dev->core, 0, sizeof(dev->core));
  788. #endif
  789. }
  790. /**
  791. * ps3_vuart_port_device_register - Add a vuart port device.
  792. */
  793. int ps3_vuart_port_device_register(struct ps3_vuart_port_device *dev)
  794. {
  795. static unsigned int dev_count = 1;
  796. BUG_ON(dev->priv && "forgot to free");
  797. dev->core.parent = NULL;
  798. dev->core.bus = &ps3_vuart_bus;
  799. dev->core.release = ps3_vuart_port_release_device;
  800. snprintf(dev->core.bus_id, sizeof(dev->core.bus_id), "vuart_%02x",
  801. dev_count++);
  802. dev_dbg(&dev->core, "%s:%d register\n", __func__, __LINE__);
  803. return device_register(&dev->core);
  804. }
  805. EXPORT_SYMBOL_GPL(ps3_vuart_port_device_register);
  806. /**
  807. * ps3_vuart_port_driver_register - Add a vuart port device driver.
  808. */
  809. int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver *drv)
  810. {
  811. int result;
  812. pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.name);
  813. drv->core.bus = &ps3_vuart_bus;
  814. result = driver_register(&drv->core);
  815. return result;
  816. }
  817. EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register);
  818. /**
  819. * ps3_vuart_port_driver_unregister - Remove a vuart port device driver.
  820. */
  821. void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver *drv)
  822. {
  823. pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.name);
  824. driver_unregister(&drv->core);
  825. }
  826. EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister);