vuart.c 27 KB

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