fwserial.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957
  1. /*
  2. * FireWire Serial driver
  3. *
  4. * Copyright (C) 2012 Peter Hurley <peter@hurleysoftware.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/sched.h>
  18. #include <linux/slab.h>
  19. #include <linux/device.h>
  20. #include <linux/mod_devicetable.h>
  21. #include <linux/rculist.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/ratelimit.h>
  24. #include <linux/bug.h>
  25. #include <linux/uaccess.h>
  26. #include "fwserial.h"
  27. #define be32_to_u64(hi, lo) ((u64)be32_to_cpu(hi) << 32 | be32_to_cpu(lo))
  28. #define LINUX_VENDOR_ID 0xd00d1eU /* same id used in card root directory */
  29. #define FWSERIAL_VERSION 0x00e81cU /* must be unique within LINUX_VENDOR_ID */
  30. /* configurable options */
  31. static int num_ttys = 4; /* # of std ttys to create per fw_card */
  32. /* - doubles as loopback port index */
  33. static bool auto_connect = true; /* try to VIRT_CABLE to every peer */
  34. static bool create_loop_dev = true; /* create a loopback device for each card */
  35. module_param_named(ttys, num_ttys, int, S_IRUGO | S_IWUSR);
  36. module_param_named(auto, auto_connect, bool, S_IRUGO | S_IWUSR);
  37. module_param_named(loop, create_loop_dev, bool, S_IRUGO | S_IWUSR);
  38. /*
  39. * Threshold below which the tty is woken for writing
  40. * - should be equal to WAKEUP_CHARS in drivers/tty/n_tty.c because
  41. * even if the writer is woken, n_tty_poll() won't set POLLOUT until
  42. * our fifo is below this level
  43. */
  44. #define WAKEUP_CHARS 256
  45. /**
  46. * fwserial_list: list of every fw_serial created for each fw_card
  47. * See discussion in fwserial_probe.
  48. */
  49. static LIST_HEAD(fwserial_list);
  50. static DEFINE_MUTEX(fwserial_list_mutex);
  51. /**
  52. * port_table: array of tty ports allocated to each fw_card
  53. *
  54. * tty ports are allocated during probe when an fw_serial is first
  55. * created for a given fw_card. Ports are allocated in a contiguous block,
  56. * each block consisting of 'num_ports' ports.
  57. */
  58. static struct fwtty_port *port_table[MAX_TOTAL_PORTS];
  59. static DEFINE_MUTEX(port_table_lock);
  60. static bool port_table_corrupt;
  61. #define FWTTY_INVALID_INDEX MAX_TOTAL_PORTS
  62. #define loop_idx(port) (((port)->index) / num_ports)
  63. #define table_idx(loop) ((loop) * num_ports + num_ttys)
  64. /* total # of tty ports created per fw_card */
  65. static int num_ports;
  66. /* slab used as pool for struct fwtty_transactions */
  67. static struct kmem_cache *fwtty_txn_cache;
  68. struct tty_driver *fwtty_driver;
  69. static struct tty_driver *fwloop_driver;
  70. static struct dentry *fwserial_debugfs;
  71. struct fwtty_transaction;
  72. typedef void (*fwtty_transaction_cb)(struct fw_card *card, int rcode,
  73. void *data, size_t length,
  74. struct fwtty_transaction *txn);
  75. struct fwtty_transaction {
  76. struct fw_transaction fw_txn;
  77. fwtty_transaction_cb callback;
  78. struct fwtty_port *port;
  79. union {
  80. struct dma_pending dma_pended;
  81. };
  82. };
  83. #define to_device(a, b) (a->b)
  84. #define fwtty_err(p, fmt, ...) \
  85. dev_err(to_device(p, device), fmt, ##__VA_ARGS__)
  86. #define fwtty_info(p, fmt, ...) \
  87. dev_info(to_device(p, device), fmt, ##__VA_ARGS__)
  88. #define fwtty_notice(p, fmt, ...) \
  89. dev_notice(to_device(p, device), fmt, ##__VA_ARGS__)
  90. #define fwtty_dbg(p, fmt, ...) \
  91. dev_dbg(to_device(p, device), "%s: " fmt, __func__, ##__VA_ARGS__)
  92. #define fwtty_err_ratelimited(p, fmt, ...) \
  93. dev_err_ratelimited(to_device(p, device), fmt, ##__VA_ARGS__)
  94. #ifdef DEBUG
  95. static inline void debug_short_write(struct fwtty_port *port, int c, int n)
  96. {
  97. int avail;
  98. if (n < c) {
  99. spin_lock_bh(&port->lock);
  100. avail = dma_fifo_avail(&port->tx_fifo);
  101. spin_unlock_bh(&port->lock);
  102. fwtty_dbg(port, "short write: avail:%d req:%d wrote:%d\n",
  103. avail, c, n);
  104. }
  105. }
  106. #else
  107. #define debug_short_write(port, c, n)
  108. #endif
  109. static struct fwtty_peer *__fwserial_peer_by_node_id(struct fw_card *card,
  110. int generation, int id);
  111. #ifdef FWTTY_PROFILING
  112. static void fwtty_profile_fifo(struct fwtty_port *port, unsigned *stat)
  113. {
  114. spin_lock_bh(&port->lock);
  115. fwtty_profile_data(stat, dma_fifo_avail(&port->tx_fifo));
  116. spin_unlock_bh(&port->lock);
  117. }
  118. static void fwtty_dump_profile(struct seq_file *m, struct stats *stats)
  119. {
  120. /* for each stat, print sum of 0 to 2^k, then individually */
  121. int k = 4;
  122. unsigned sum;
  123. int j;
  124. char t[10];
  125. snprintf(t, 10, "< %d", 1 << k);
  126. seq_printf(m, "\n%14s %6s", " ", t);
  127. for (j = k + 1; j < DISTRIBUTION_MAX_INDEX; ++j)
  128. seq_printf(m, "%6d", 1 << j);
  129. ++k;
  130. for (j = 0, sum = 0; j <= k; ++j)
  131. sum += stats->reads[j];
  132. seq_printf(m, "\n%14s: %6d", "reads", sum);
  133. for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
  134. seq_printf(m, "%6d", stats->reads[j]);
  135. for (j = 0, sum = 0; j <= k; ++j)
  136. sum += stats->writes[j];
  137. seq_printf(m, "\n%14s: %6d", "writes", sum);
  138. for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
  139. seq_printf(m, "%6d", stats->writes[j]);
  140. for (j = 0, sum = 0; j <= k; ++j)
  141. sum += stats->txns[j];
  142. seq_printf(m, "\n%14s: %6d", "txns", sum);
  143. for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
  144. seq_printf(m, "%6d", stats->txns[j]);
  145. for (j = 0, sum = 0; j <= k; ++j)
  146. sum += stats->unthrottle[j];
  147. seq_printf(m, "\n%14s: %6d", "avail @ unthr", sum);
  148. for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
  149. seq_printf(m, "%6d", stats->unthrottle[j]);
  150. }
  151. #else
  152. #define fwtty_profile_fifo(port, stat)
  153. #define fwtty_dump_profile(m, stats)
  154. #endif
  155. /*
  156. * Returns the max receive packet size for the given node
  157. * Devices which are OHCI v1.0/ v1.1/ v1.2-draft or RFC 2734 compliant
  158. * are required by specification to support max_rec of 8 (512 bytes) or more.
  159. */
  160. static inline int device_max_receive(struct fw_device *fw_device)
  161. {
  162. /* see IEEE 1394-2008 table 8-8 */
  163. return min(2 << fw_device->max_rec, 4096);
  164. }
  165. static void fwtty_log_tx_error(struct fwtty_port *port, int rcode)
  166. {
  167. switch (rcode) {
  168. case RCODE_SEND_ERROR:
  169. fwtty_err_ratelimited(port, "card busy\n");
  170. break;
  171. case RCODE_ADDRESS_ERROR:
  172. fwtty_err_ratelimited(port, "bad unit addr or write length\n");
  173. break;
  174. case RCODE_DATA_ERROR:
  175. fwtty_err_ratelimited(port, "failed rx\n");
  176. break;
  177. case RCODE_NO_ACK:
  178. fwtty_err_ratelimited(port, "missing ack\n");
  179. break;
  180. case RCODE_BUSY:
  181. fwtty_err_ratelimited(port, "remote busy\n");
  182. break;
  183. default:
  184. fwtty_err_ratelimited(port, "failed tx: %d\n", rcode);
  185. }
  186. }
  187. static void fwtty_txn_constructor(void *this)
  188. {
  189. struct fwtty_transaction *txn = this;
  190. init_timer(&txn->fw_txn.split_timeout_timer);
  191. }
  192. static void fwtty_common_callback(struct fw_card *card, int rcode,
  193. void *payload, size_t len, void *cb_data)
  194. {
  195. struct fwtty_transaction *txn = cb_data;
  196. struct fwtty_port *port = txn->port;
  197. if (port && rcode != RCODE_COMPLETE)
  198. fwtty_log_tx_error(port, rcode);
  199. if (txn->callback)
  200. txn->callback(card, rcode, payload, len, txn);
  201. kmem_cache_free(fwtty_txn_cache, txn);
  202. }
  203. static int fwtty_send_data_async(struct fwtty_peer *peer, int tcode,
  204. unsigned long long addr, void *payload,
  205. size_t len, fwtty_transaction_cb callback,
  206. struct fwtty_port *port)
  207. {
  208. struct fwtty_transaction *txn;
  209. int generation;
  210. txn = kmem_cache_alloc(fwtty_txn_cache, GFP_ATOMIC);
  211. if (!txn)
  212. return -ENOMEM;
  213. txn->callback = callback;
  214. txn->port = port;
  215. generation = peer->generation;
  216. smp_rmb();
  217. fw_send_request(peer->serial->card, &txn->fw_txn, tcode,
  218. peer->node_id, generation, peer->speed, addr, payload,
  219. len, fwtty_common_callback, txn);
  220. return 0;
  221. }
  222. static void fwtty_send_txn_async(struct fwtty_peer *peer,
  223. struct fwtty_transaction *txn, int tcode,
  224. unsigned long long addr, void *payload,
  225. size_t len, fwtty_transaction_cb callback,
  226. struct fwtty_port *port)
  227. {
  228. int generation;
  229. txn->callback = callback;
  230. txn->port = port;
  231. generation = peer->generation;
  232. smp_rmb();
  233. fw_send_request(peer->serial->card, &txn->fw_txn, tcode,
  234. peer->node_id, generation, peer->speed, addr, payload,
  235. len, fwtty_common_callback, txn);
  236. }
  237. static void __fwtty_restart_tx(struct fwtty_port *port)
  238. {
  239. int len, avail;
  240. len = dma_fifo_out_level(&port->tx_fifo);
  241. if (len)
  242. schedule_delayed_work(&port->drain, 0);
  243. avail = dma_fifo_avail(&port->tx_fifo);
  244. fwtty_dbg(port, "fifo len: %d avail: %d\n", len, avail);
  245. }
  246. static void fwtty_restart_tx(struct fwtty_port *port)
  247. {
  248. spin_lock_bh(&port->lock);
  249. __fwtty_restart_tx(port);
  250. spin_unlock_bh(&port->lock);
  251. }
  252. /**
  253. * fwtty_update_port_status - decodes & dispatches line status changes
  254. *
  255. * Note: in loopback, the port->lock is being held. Only use functions that
  256. * don't attempt to reclaim the port->lock.
  257. */
  258. static void fwtty_update_port_status(struct fwtty_port *port, unsigned status)
  259. {
  260. unsigned delta;
  261. struct tty_struct *tty;
  262. /* simulated LSR/MSR status from remote */
  263. status &= ~MCTRL_MASK;
  264. delta = (port->mstatus ^ status) & ~MCTRL_MASK;
  265. delta &= ~(status & TIOCM_RNG);
  266. port->mstatus = status;
  267. if (delta & TIOCM_RNG)
  268. ++port->icount.rng;
  269. if (delta & TIOCM_DSR)
  270. ++port->icount.dsr;
  271. if (delta & TIOCM_CAR)
  272. ++port->icount.dcd;
  273. if (delta & TIOCM_CTS)
  274. ++port->icount.cts;
  275. fwtty_dbg(port, "status: %x delta: %x\n", status, delta);
  276. if (delta & TIOCM_CAR) {
  277. tty = tty_port_tty_get(&port->port);
  278. if (tty && !C_CLOCAL(tty)) {
  279. if (status & TIOCM_CAR)
  280. wake_up_interruptible(&port->port.open_wait);
  281. else
  282. schedule_work(&port->hangup);
  283. }
  284. tty_kref_put(tty);
  285. }
  286. if (delta & TIOCM_CTS) {
  287. tty = tty_port_tty_get(&port->port);
  288. if (tty && C_CRTSCTS(tty)) {
  289. if (tty->hw_stopped) {
  290. if (status & TIOCM_CTS) {
  291. tty->hw_stopped = 0;
  292. if (port->loopback)
  293. __fwtty_restart_tx(port);
  294. else
  295. fwtty_restart_tx(port);
  296. }
  297. } else {
  298. if (~status & TIOCM_CTS)
  299. tty->hw_stopped = 1;
  300. }
  301. }
  302. tty_kref_put(tty);
  303. } else if (delta & OOB_TX_THROTTLE) {
  304. tty = tty_port_tty_get(&port->port);
  305. if (tty) {
  306. if (tty->hw_stopped) {
  307. if (~status & OOB_TX_THROTTLE) {
  308. tty->hw_stopped = 0;
  309. if (port->loopback)
  310. __fwtty_restart_tx(port);
  311. else
  312. fwtty_restart_tx(port);
  313. }
  314. } else {
  315. if (status & OOB_TX_THROTTLE)
  316. tty->hw_stopped = 1;
  317. }
  318. }
  319. tty_kref_put(tty);
  320. }
  321. if (delta & (UART_LSR_BI << 24)) {
  322. if (status & (UART_LSR_BI << 24)) {
  323. port->break_last = jiffies;
  324. schedule_delayed_work(&port->emit_breaks, 0);
  325. } else {
  326. /* run emit_breaks one last time (if pending) */
  327. mod_delayed_work(system_wq, &port->emit_breaks, 0);
  328. }
  329. }
  330. if (delta & (TIOCM_DSR | TIOCM_CAR | TIOCM_CTS | TIOCM_RNG))
  331. wake_up_interruptible(&port->port.delta_msr_wait);
  332. }
  333. /**
  334. * __fwtty_port_line_status - generate 'line status' for indicated port
  335. *
  336. * This function returns a remote 'MSR' state based on the local 'MCR' state,
  337. * as if a null modem cable was attached. The actual status is a mangling
  338. * of TIOCM_* bits suitable for sending to a peer's status_addr.
  339. *
  340. * Note: caller must be holding port lock
  341. */
  342. static unsigned __fwtty_port_line_status(struct fwtty_port *port)
  343. {
  344. unsigned status = 0;
  345. /* TODO: add module param to tie RNG to DTR as well */
  346. if (port->mctrl & TIOCM_DTR)
  347. status |= TIOCM_DSR | TIOCM_CAR;
  348. if (port->mctrl & TIOCM_RTS)
  349. status |= TIOCM_CTS;
  350. if (port->mctrl & OOB_RX_THROTTLE)
  351. status |= OOB_TX_THROTTLE;
  352. /* emulate BRK as add'l line status */
  353. if (port->break_ctl)
  354. status |= UART_LSR_BI << 24;
  355. return status;
  356. }
  357. /**
  358. * __fwtty_write_port_status - send the port line status to peer
  359. *
  360. * Note: caller must be holding the port lock.
  361. */
  362. static int __fwtty_write_port_status(struct fwtty_port *port)
  363. {
  364. struct fwtty_peer *peer;
  365. int err = -ENOENT;
  366. unsigned status = __fwtty_port_line_status(port);
  367. rcu_read_lock();
  368. peer = rcu_dereference(port->peer);
  369. if (peer) {
  370. err = fwtty_send_data_async(peer, TCODE_WRITE_QUADLET_REQUEST,
  371. peer->status_addr, &status,
  372. sizeof(status), NULL, port);
  373. }
  374. rcu_read_unlock();
  375. return err;
  376. }
  377. /**
  378. * fwtty_write_port_status - same as above but locked by port lock
  379. */
  380. static int fwtty_write_port_status(struct fwtty_port *port)
  381. {
  382. int err;
  383. spin_lock_bh(&port->lock);
  384. err = __fwtty_write_port_status(port);
  385. spin_unlock_bh(&port->lock);
  386. return err;
  387. }
  388. static void fwtty_throttle_port(struct fwtty_port *port)
  389. {
  390. struct tty_struct *tty;
  391. unsigned old;
  392. tty = tty_port_tty_get(&port->port);
  393. if (!tty)
  394. return;
  395. spin_lock_bh(&port->lock);
  396. old = port->mctrl;
  397. port->mctrl |= OOB_RX_THROTTLE;
  398. if (C_CRTSCTS(tty))
  399. port->mctrl &= ~TIOCM_RTS;
  400. if (~old & OOB_RX_THROTTLE)
  401. __fwtty_write_port_status(port);
  402. spin_unlock_bh(&port->lock);
  403. tty_kref_put(tty);
  404. }
  405. /**
  406. * fwtty_do_hangup - wait for ldisc to deliver all pending rx; only then hangup
  407. *
  408. * When the remote has finished tx, and all in-flight rx has been received and
  409. * and pushed to the flip buffer, the remote may close its device. This will
  410. * drop DTR on the remote which will drop carrier here. Typically, the tty is
  411. * hung up when carrier is dropped or lost.
  412. *
  413. * However, there is a race between the hang up and the line discipline
  414. * delivering its data to the reader. A hangup will cause the ldisc to flush
  415. * (ie., clear) the read buffer and flip buffer. Because of firewire's
  416. * relatively high throughput, the ldisc frequently lags well behind the driver,
  417. * resulting in lost data (which has already been received and written to
  418. * the flip buffer) when the remote closes its end.
  419. *
  420. * Unfortunately, since the flip buffer offers no direct method for determining
  421. * if it holds data, ensuring the ldisc has delivered all data is problematic.
  422. */
  423. /* FIXME: drop this workaround when __tty_hangup waits for ldisc completion */
  424. static void fwtty_do_hangup(struct work_struct *work)
  425. {
  426. struct fwtty_port *port = to_port(work, hangup);
  427. struct tty_struct *tty;
  428. schedule_timeout_uninterruptible(msecs_to_jiffies(50));
  429. tty = tty_port_tty_get(&port->port);
  430. if (tty)
  431. tty_vhangup(tty);
  432. tty_kref_put(tty);
  433. }
  434. static void fwtty_emit_breaks(struct work_struct *work)
  435. {
  436. struct fwtty_port *port = to_port(to_delayed_work(work), emit_breaks);
  437. static const char buf[16];
  438. unsigned long now = jiffies;
  439. unsigned long elapsed = now - port->break_last;
  440. int n, t, c, brk = 0;
  441. /* generate breaks at the line rate (but at least 1) */
  442. n = (elapsed * port->cps) / HZ + 1;
  443. port->break_last = now;
  444. fwtty_dbg(port, "sending %d brks\n", n);
  445. while (n) {
  446. t = min(n, 16);
  447. c = tty_insert_flip_string_fixed_flag(&port->port, buf,
  448. TTY_BREAK, t);
  449. n -= c;
  450. brk += c;
  451. if (c < t)
  452. break;
  453. }
  454. tty_flip_buffer_push(&port->port);
  455. if (port->mstatus & (UART_LSR_BI << 24))
  456. schedule_delayed_work(&port->emit_breaks, FREQ_BREAKS);
  457. port->icount.brk += brk;
  458. }
  459. static int fwtty_rx(struct fwtty_port *port, unsigned char *data, size_t len)
  460. {
  461. int c, n = len;
  462. unsigned lsr;
  463. int err = 0;
  464. fwtty_dbg(port, "%d\n", n);
  465. fwtty_profile_data(port->stats.reads, n);
  466. if (port->write_only) {
  467. n = 0;
  468. goto out;
  469. }
  470. /* disregard break status; breaks are generated by emit_breaks work */
  471. lsr = (port->mstatus >> 24) & ~UART_LSR_BI;
  472. if (port->overrun)
  473. lsr |= UART_LSR_OE;
  474. if (lsr & UART_LSR_OE)
  475. ++port->icount.overrun;
  476. lsr &= port->status_mask;
  477. if (lsr & ~port->ignore_mask & UART_LSR_OE) {
  478. if (!tty_insert_flip_char(&port->port, 0, TTY_OVERRUN)) {
  479. err = -EIO;
  480. goto out;
  481. }
  482. }
  483. port->overrun = false;
  484. if (lsr & port->ignore_mask & ~UART_LSR_OE) {
  485. /* TODO: don't drop SAK and Magic SysRq here */
  486. n = 0;
  487. goto out;
  488. }
  489. c = tty_insert_flip_string_fixed_flag(&port->port, data, TTY_NORMAL, n);
  490. if (c > 0)
  491. tty_flip_buffer_push(&port->port);
  492. n -= c;
  493. if (n) {
  494. port->overrun = true;
  495. err = -EIO;
  496. fwtty_err_ratelimited(port, "flip buffer overrun\n");
  497. } else {
  498. /* throttle the sender if remaining flip buffer space has
  499. * reached high watermark to avoid losing data which may be
  500. * in-flight. Since the AR request context is 32k, that much
  501. * data may have _already_ been acked.
  502. */
  503. if (tty_buffer_space_avail(&port->port) < HIGH_WATERMARK)
  504. fwtty_throttle_port(port);
  505. }
  506. out:
  507. port->icount.rx += len;
  508. port->stats.lost += n;
  509. return err;
  510. }
  511. /**
  512. * fwtty_port_handler - bus address handler for port reads/writes
  513. * @parameters: fw_address_callback_t as specified by firewire core interface
  514. *
  515. * This handler is responsible for handling inbound read/write dma from remotes.
  516. */
  517. static void fwtty_port_handler(struct fw_card *card,
  518. struct fw_request *request,
  519. int tcode, int destination, int source,
  520. int generation,
  521. unsigned long long addr,
  522. void *data, size_t len,
  523. void *callback_data)
  524. {
  525. struct fwtty_port *port = callback_data;
  526. struct fwtty_peer *peer;
  527. int err;
  528. int rcode;
  529. /* Only accept rx from the peer virtual-cabled to this port */
  530. rcu_read_lock();
  531. peer = __fwserial_peer_by_node_id(card, generation, source);
  532. rcu_read_unlock();
  533. if (!peer || peer != rcu_access_pointer(port->peer)) {
  534. rcode = RCODE_ADDRESS_ERROR;
  535. fwtty_err_ratelimited(port, "ignoring unauthenticated data\n");
  536. goto respond;
  537. }
  538. switch (tcode) {
  539. case TCODE_WRITE_QUADLET_REQUEST:
  540. if (addr != port->rx_handler.offset || len != 4) {
  541. rcode = RCODE_ADDRESS_ERROR;
  542. } else {
  543. fwtty_update_port_status(port, *(unsigned *)data);
  544. rcode = RCODE_COMPLETE;
  545. }
  546. break;
  547. case TCODE_WRITE_BLOCK_REQUEST:
  548. if (addr != port->rx_handler.offset + 4 ||
  549. len > port->rx_handler.length - 4) {
  550. rcode = RCODE_ADDRESS_ERROR;
  551. } else {
  552. err = fwtty_rx(port, data, len);
  553. switch (err) {
  554. case 0:
  555. rcode = RCODE_COMPLETE;
  556. break;
  557. case -EIO:
  558. rcode = RCODE_DATA_ERROR;
  559. break;
  560. default:
  561. rcode = RCODE_CONFLICT_ERROR;
  562. break;
  563. }
  564. }
  565. break;
  566. default:
  567. rcode = RCODE_TYPE_ERROR;
  568. }
  569. respond:
  570. fw_send_response(card, request, rcode);
  571. }
  572. /**
  573. * fwtty_tx_complete - callback for tx dma
  574. * @data: ignored, has no meaning for write txns
  575. * @length: ignored, has no meaning for write txns
  576. *
  577. * The writer must be woken here if the fifo has been emptied because it
  578. * may have slept if chars_in_buffer was != 0
  579. */
  580. static void fwtty_tx_complete(struct fw_card *card, int rcode,
  581. void *data, size_t length,
  582. struct fwtty_transaction *txn)
  583. {
  584. struct fwtty_port *port = txn->port;
  585. int len;
  586. fwtty_dbg(port, "rcode: %d\n", rcode);
  587. switch (rcode) {
  588. case RCODE_COMPLETE:
  589. spin_lock_bh(&port->lock);
  590. dma_fifo_out_complete(&port->tx_fifo, &txn->dma_pended);
  591. len = dma_fifo_level(&port->tx_fifo);
  592. spin_unlock_bh(&port->lock);
  593. port->icount.tx += txn->dma_pended.len;
  594. break;
  595. default:
  596. /* TODO: implement retries */
  597. spin_lock_bh(&port->lock);
  598. dma_fifo_out_complete(&port->tx_fifo, &txn->dma_pended);
  599. len = dma_fifo_level(&port->tx_fifo);
  600. spin_unlock_bh(&port->lock);
  601. port->stats.dropped += txn->dma_pended.len;
  602. }
  603. if (len < WAKEUP_CHARS)
  604. tty_port_tty_wakeup(&port->port);
  605. }
  606. static int fwtty_tx(struct fwtty_port *port, bool drain)
  607. {
  608. struct fwtty_peer *peer;
  609. struct fwtty_transaction *txn;
  610. struct tty_struct *tty;
  611. int n, len;
  612. tty = tty_port_tty_get(&port->port);
  613. if (!tty)
  614. return -ENOENT;
  615. rcu_read_lock();
  616. peer = rcu_dereference(port->peer);
  617. if (!peer) {
  618. n = -EIO;
  619. goto out;
  620. }
  621. if (test_and_set_bit(IN_TX, &port->flags)) {
  622. n = -EALREADY;
  623. goto out;
  624. }
  625. /* try to write as many dma transactions out as possible */
  626. n = -EAGAIN;
  627. while (!tty->stopped && !tty->hw_stopped &&
  628. !test_bit(STOP_TX, &port->flags)) {
  629. txn = kmem_cache_alloc(fwtty_txn_cache, GFP_ATOMIC);
  630. if (!txn) {
  631. n = -ENOMEM;
  632. break;
  633. }
  634. spin_lock_bh(&port->lock);
  635. n = dma_fifo_out_pend(&port->tx_fifo, &txn->dma_pended);
  636. spin_unlock_bh(&port->lock);
  637. fwtty_dbg(port, "out: %u rem: %d\n", txn->dma_pended.len, n);
  638. if (n < 0) {
  639. kmem_cache_free(fwtty_txn_cache, txn);
  640. if (n == -EAGAIN) {
  641. ++port->stats.tx_stall;
  642. } else if (n == -ENODATA) {
  643. fwtty_profile_data(port->stats.txns, 0);
  644. } else {
  645. ++port->stats.fifo_errs;
  646. fwtty_err_ratelimited(port, "fifo err: %d\n",
  647. n);
  648. }
  649. break;
  650. }
  651. fwtty_profile_data(port->stats.txns, txn->dma_pended.len);
  652. fwtty_send_txn_async(peer, txn, TCODE_WRITE_BLOCK_REQUEST,
  653. peer->fifo_addr, txn->dma_pended.data,
  654. txn->dma_pended.len, fwtty_tx_complete,
  655. port);
  656. ++port->stats.sent;
  657. /*
  658. * Stop tx if the 'last view' of the fifo is empty or if
  659. * this is the writer and there's not enough data to bother
  660. */
  661. if (n == 0 || (!drain && n < WRITER_MINIMUM))
  662. break;
  663. }
  664. if (n >= 0 || n == -EAGAIN || n == -ENOMEM || n == -ENODATA) {
  665. spin_lock_bh(&port->lock);
  666. len = dma_fifo_out_level(&port->tx_fifo);
  667. if (len) {
  668. unsigned long delay = (n == -ENOMEM) ? HZ : 1;
  669. schedule_delayed_work(&port->drain, delay);
  670. }
  671. len = dma_fifo_level(&port->tx_fifo);
  672. spin_unlock_bh(&port->lock);
  673. /* wakeup the writer */
  674. if (drain && len < WAKEUP_CHARS)
  675. tty_wakeup(tty);
  676. }
  677. clear_bit(IN_TX, &port->flags);
  678. wake_up_interruptible(&port->wait_tx);
  679. out:
  680. rcu_read_unlock();
  681. tty_kref_put(tty);
  682. return n;
  683. }
  684. static void fwtty_drain_tx(struct work_struct *work)
  685. {
  686. struct fwtty_port *port = to_port(to_delayed_work(work), drain);
  687. fwtty_tx(port, true);
  688. }
  689. static void fwtty_write_xchar(struct fwtty_port *port, char ch)
  690. {
  691. struct fwtty_peer *peer;
  692. ++port->stats.xchars;
  693. fwtty_dbg(port, "%02x\n", ch);
  694. rcu_read_lock();
  695. peer = rcu_dereference(port->peer);
  696. if (peer) {
  697. fwtty_send_data_async(peer, TCODE_WRITE_BLOCK_REQUEST,
  698. peer->fifo_addr, &ch, sizeof(ch),
  699. NULL, port);
  700. }
  701. rcu_read_unlock();
  702. }
  703. struct fwtty_port *fwtty_port_get(unsigned index)
  704. {
  705. struct fwtty_port *port;
  706. if (index >= MAX_TOTAL_PORTS)
  707. return NULL;
  708. mutex_lock(&port_table_lock);
  709. port = port_table[index];
  710. if (port)
  711. kref_get(&port->serial->kref);
  712. mutex_unlock(&port_table_lock);
  713. return port;
  714. }
  715. EXPORT_SYMBOL(fwtty_port_get);
  716. static int fwtty_ports_add(struct fw_serial *serial)
  717. {
  718. int err = -EBUSY;
  719. int i, j;
  720. if (port_table_corrupt)
  721. return err;
  722. mutex_lock(&port_table_lock);
  723. for (i = 0; i + num_ports <= MAX_TOTAL_PORTS; i += num_ports) {
  724. if (!port_table[i]) {
  725. for (j = 0; j < num_ports; ++i, ++j) {
  726. serial->ports[j]->index = i;
  727. port_table[i] = serial->ports[j];
  728. }
  729. err = 0;
  730. break;
  731. }
  732. }
  733. mutex_unlock(&port_table_lock);
  734. return err;
  735. }
  736. static void fwserial_destroy(struct kref *kref)
  737. {
  738. struct fw_serial *serial = to_serial(kref, kref);
  739. struct fwtty_port **ports = serial->ports;
  740. int j, i = ports[0]->index;
  741. synchronize_rcu();
  742. mutex_lock(&port_table_lock);
  743. for (j = 0; j < num_ports; ++i, ++j) {
  744. port_table_corrupt |= port_table[i] != ports[j];
  745. WARN_ONCE(port_table_corrupt, "port_table[%d]: %p != ports[%d]: %p",
  746. i, port_table[i], j, ports[j]);
  747. port_table[i] = NULL;
  748. }
  749. mutex_unlock(&port_table_lock);
  750. for (j = 0; j < num_ports; ++j) {
  751. fw_core_remove_address_handler(&ports[j]->rx_handler);
  752. tty_port_destroy(&ports[j]->port);
  753. kfree(ports[j]);
  754. }
  755. kfree(serial);
  756. }
  757. void fwtty_port_put(struct fwtty_port *port)
  758. {
  759. kref_put(&port->serial->kref, fwserial_destroy);
  760. }
  761. EXPORT_SYMBOL(fwtty_port_put);
  762. static void fwtty_port_dtr_rts(struct tty_port *tty_port, int on)
  763. {
  764. struct fwtty_port *port = to_port(tty_port, port);
  765. fwtty_dbg(port, "on/off: %d\n", on);
  766. spin_lock_bh(&port->lock);
  767. /* Don't change carrier state if this is a console */
  768. if (!port->port.console) {
  769. if (on)
  770. port->mctrl |= TIOCM_DTR | TIOCM_RTS;
  771. else
  772. port->mctrl &= ~(TIOCM_DTR | TIOCM_RTS);
  773. }
  774. __fwtty_write_port_status(port);
  775. spin_unlock_bh(&port->lock);
  776. }
  777. /**
  778. * fwtty_port_carrier_raised: required tty_port operation
  779. *
  780. * This port operation is polled after a tty has been opened and is waiting for
  781. * carrier detect -- see drivers/tty/tty_port:tty_port_block_til_ready().
  782. */
  783. static int fwtty_port_carrier_raised(struct tty_port *tty_port)
  784. {
  785. struct fwtty_port *port = to_port(tty_port, port);
  786. int rc;
  787. rc = (port->mstatus & TIOCM_CAR);
  788. fwtty_dbg(port, "%d\n", rc);
  789. return rc;
  790. }
  791. static unsigned set_termios(struct fwtty_port *port, struct tty_struct *tty)
  792. {
  793. unsigned baud, frame;
  794. baud = tty_termios_baud_rate(&tty->termios);
  795. tty_termios_encode_baud_rate(&tty->termios, baud, baud);
  796. /* compute bit count of 2 frames */
  797. frame = 12 + ((C_CSTOPB(tty)) ? 4 : 2) + ((C_PARENB(tty)) ? 2 : 0);
  798. switch (C_CSIZE(tty)) {
  799. case CS5:
  800. frame -= (C_CSTOPB(tty)) ? 1 : 0;
  801. break;
  802. case CS6:
  803. frame += 2;
  804. break;
  805. case CS7:
  806. frame += 4;
  807. break;
  808. case CS8:
  809. frame += 6;
  810. break;
  811. }
  812. port->cps = (baud << 1) / frame;
  813. port->status_mask = UART_LSR_OE;
  814. if (_I_FLAG(tty, BRKINT | PARMRK))
  815. port->status_mask |= UART_LSR_BI;
  816. port->ignore_mask = 0;
  817. if (I_IGNBRK(tty)) {
  818. port->ignore_mask |= UART_LSR_BI;
  819. if (I_IGNPAR(tty))
  820. port->ignore_mask |= UART_LSR_OE;
  821. }
  822. port->write_only = !C_CREAD(tty);
  823. /* turn off echo and newline xlat if loopback */
  824. if (port->loopback) {
  825. tty->termios.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHOKE |
  826. ECHONL | ECHOPRT | ECHOCTL);
  827. tty->termios.c_oflag &= ~ONLCR;
  828. }
  829. return baud;
  830. }
  831. static int fwtty_port_activate(struct tty_port *tty_port,
  832. struct tty_struct *tty)
  833. {
  834. struct fwtty_port *port = to_port(tty_port, port);
  835. unsigned baud;
  836. int err;
  837. set_bit(TTY_IO_ERROR, &tty->flags);
  838. err = dma_fifo_alloc(&port->tx_fifo, FWTTY_PORT_TXFIFO_LEN,
  839. cache_line_size(),
  840. port->max_payload,
  841. FWTTY_PORT_MAX_PEND_DMA,
  842. GFP_KERNEL);
  843. if (err)
  844. return err;
  845. spin_lock_bh(&port->lock);
  846. baud = set_termios(port, tty);
  847. /* if console, don't change carrier state */
  848. if (!port->port.console) {
  849. port->mctrl = 0;
  850. if (baud != 0)
  851. port->mctrl = TIOCM_DTR | TIOCM_RTS;
  852. }
  853. if (C_CRTSCTS(tty) && ~port->mstatus & TIOCM_CTS)
  854. tty->hw_stopped = 1;
  855. __fwtty_write_port_status(port);
  856. spin_unlock_bh(&port->lock);
  857. clear_bit(TTY_IO_ERROR, &tty->flags);
  858. return 0;
  859. }
  860. /**
  861. * fwtty_port_shutdown
  862. *
  863. * Note: the tty port core ensures this is not the console and
  864. * manages TTY_IO_ERROR properly
  865. */
  866. static void fwtty_port_shutdown(struct tty_port *tty_port)
  867. {
  868. struct fwtty_port *port = to_port(tty_port, port);
  869. /* TODO: cancel outstanding transactions */
  870. cancel_delayed_work_sync(&port->emit_breaks);
  871. cancel_delayed_work_sync(&port->drain);
  872. spin_lock_bh(&port->lock);
  873. port->flags = 0;
  874. port->break_ctl = 0;
  875. port->overrun = 0;
  876. __fwtty_write_port_status(port);
  877. dma_fifo_free(&port->tx_fifo);
  878. spin_unlock_bh(&port->lock);
  879. }
  880. static int fwtty_open(struct tty_struct *tty, struct file *fp)
  881. {
  882. struct fwtty_port *port = tty->driver_data;
  883. return tty_port_open(&port->port, tty, fp);
  884. }
  885. static void fwtty_close(struct tty_struct *tty, struct file *fp)
  886. {
  887. struct fwtty_port *port = tty->driver_data;
  888. tty_port_close(&port->port, tty, fp);
  889. }
  890. static void fwtty_hangup(struct tty_struct *tty)
  891. {
  892. struct fwtty_port *port = tty->driver_data;
  893. tty_port_hangup(&port->port);
  894. }
  895. static void fwtty_cleanup(struct tty_struct *tty)
  896. {
  897. struct fwtty_port *port = tty->driver_data;
  898. tty->driver_data = NULL;
  899. fwtty_port_put(port);
  900. }
  901. static int fwtty_install(struct tty_driver *driver, struct tty_struct *tty)
  902. {
  903. struct fwtty_port *port = fwtty_port_get(tty->index);
  904. int err;
  905. err = tty_standard_install(driver, tty);
  906. if (!err)
  907. tty->driver_data = port;
  908. else
  909. fwtty_port_put(port);
  910. return err;
  911. }
  912. static int fwloop_install(struct tty_driver *driver, struct tty_struct *tty)
  913. {
  914. struct fwtty_port *port = fwtty_port_get(table_idx(tty->index));
  915. int err;
  916. err = tty_standard_install(driver, tty);
  917. if (!err)
  918. tty->driver_data = port;
  919. else
  920. fwtty_port_put(port);
  921. return err;
  922. }
  923. static int fwtty_write(struct tty_struct *tty, const unsigned char *buf, int c)
  924. {
  925. struct fwtty_port *port = tty->driver_data;
  926. int n, len;
  927. fwtty_dbg(port, "%d\n", c);
  928. fwtty_profile_data(port->stats.writes, c);
  929. spin_lock_bh(&port->lock);
  930. n = dma_fifo_in(&port->tx_fifo, buf, c);
  931. len = dma_fifo_out_level(&port->tx_fifo);
  932. if (len < DRAIN_THRESHOLD)
  933. schedule_delayed_work(&port->drain, 1);
  934. spin_unlock_bh(&port->lock);
  935. if (len >= DRAIN_THRESHOLD)
  936. fwtty_tx(port, false);
  937. debug_short_write(port, c, n);
  938. return (n < 0) ? 0 : n;
  939. }
  940. static int fwtty_write_room(struct tty_struct *tty)
  941. {
  942. struct fwtty_port *port = tty->driver_data;
  943. int n;
  944. spin_lock_bh(&port->lock);
  945. n = dma_fifo_avail(&port->tx_fifo);
  946. spin_unlock_bh(&port->lock);
  947. fwtty_dbg(port, "%d\n", n);
  948. return n;
  949. }
  950. static int fwtty_chars_in_buffer(struct tty_struct *tty)
  951. {
  952. struct fwtty_port *port = tty->driver_data;
  953. int n;
  954. spin_lock_bh(&port->lock);
  955. n = dma_fifo_level(&port->tx_fifo);
  956. spin_unlock_bh(&port->lock);
  957. fwtty_dbg(port, "%d\n", n);
  958. return n;
  959. }
  960. static void fwtty_send_xchar(struct tty_struct *tty, char ch)
  961. {
  962. struct fwtty_port *port = tty->driver_data;
  963. fwtty_dbg(port, "%02x\n", ch);
  964. fwtty_write_xchar(port, ch);
  965. }
  966. static void fwtty_throttle(struct tty_struct *tty)
  967. {
  968. struct fwtty_port *port = tty->driver_data;
  969. /*
  970. * Ignore throttling (but not unthrottling).
  971. * It only makes sense to throttle when data will no longer be
  972. * accepted by the tty flip buffer. For example, it is
  973. * possible for received data to overflow the tty buffer long
  974. * before the line discipline ever has a chance to throttle the driver.
  975. * Additionally, the driver may have already completed the I/O
  976. * but the tty buffer is still emptying, so the line discipline is
  977. * throttling and unthrottling nothing.
  978. */
  979. ++port->stats.throttled;
  980. }
  981. static void fwtty_unthrottle(struct tty_struct *tty)
  982. {
  983. struct fwtty_port *port = tty->driver_data;
  984. fwtty_dbg(port, "CRTSCTS: %d\n", C_CRTSCTS(tty) != 0);
  985. fwtty_profile_fifo(port, port->stats.unthrottle);
  986. spin_lock_bh(&port->lock);
  987. port->mctrl &= ~OOB_RX_THROTTLE;
  988. if (C_CRTSCTS(tty))
  989. port->mctrl |= TIOCM_RTS;
  990. __fwtty_write_port_status(port);
  991. spin_unlock_bh(&port->lock);
  992. }
  993. static int check_msr_delta(struct fwtty_port *port, unsigned long mask,
  994. struct async_icount *prev)
  995. {
  996. struct async_icount now;
  997. int delta;
  998. now = port->icount;
  999. delta = ((mask & TIOCM_RNG && prev->rng != now.rng) ||
  1000. (mask & TIOCM_DSR && prev->dsr != now.dsr) ||
  1001. (mask & TIOCM_CAR && prev->dcd != now.dcd) ||
  1002. (mask & TIOCM_CTS && prev->cts != now.cts));
  1003. *prev = now;
  1004. return delta;
  1005. }
  1006. static int wait_msr_change(struct fwtty_port *port, unsigned long mask)
  1007. {
  1008. struct async_icount prev;
  1009. prev = port->icount;
  1010. return wait_event_interruptible(port->port.delta_msr_wait,
  1011. check_msr_delta(port, mask, &prev));
  1012. }
  1013. static int get_serial_info(struct fwtty_port *port,
  1014. struct serial_struct __user *info)
  1015. {
  1016. struct serial_struct tmp;
  1017. memset(&tmp, 0, sizeof(tmp));
  1018. tmp.type = PORT_UNKNOWN;
  1019. tmp.line = port->port.tty->index;
  1020. tmp.flags = port->port.flags;
  1021. tmp.xmit_fifo_size = FWTTY_PORT_TXFIFO_LEN;
  1022. tmp.baud_base = 400000000;
  1023. tmp.close_delay = port->port.close_delay;
  1024. return (copy_to_user(info, &tmp, sizeof(*info))) ? -EFAULT : 0;
  1025. }
  1026. static int set_serial_info(struct fwtty_port *port,
  1027. struct serial_struct __user *info)
  1028. {
  1029. struct serial_struct tmp;
  1030. if (copy_from_user(&tmp, info, sizeof(tmp)))
  1031. return -EFAULT;
  1032. if (tmp.irq != 0 || tmp.port != 0 || tmp.custom_divisor != 0 ||
  1033. tmp.baud_base != 400000000)
  1034. return -EPERM;
  1035. if (!capable(CAP_SYS_ADMIN)) {
  1036. if (((tmp.flags & ~ASYNC_USR_MASK) !=
  1037. (port->port.flags & ~ASYNC_USR_MASK)))
  1038. return -EPERM;
  1039. } else {
  1040. port->port.close_delay = tmp.close_delay * HZ / 100;
  1041. }
  1042. return 0;
  1043. }
  1044. static int fwtty_ioctl(struct tty_struct *tty, unsigned cmd,
  1045. unsigned long arg)
  1046. {
  1047. struct fwtty_port *port = tty->driver_data;
  1048. int err;
  1049. switch (cmd) {
  1050. case TIOCGSERIAL:
  1051. mutex_lock(&port->port.mutex);
  1052. err = get_serial_info(port, (void __user *)arg);
  1053. mutex_unlock(&port->port.mutex);
  1054. break;
  1055. case TIOCSSERIAL:
  1056. mutex_lock(&port->port.mutex);
  1057. err = set_serial_info(port, (void __user *)arg);
  1058. mutex_unlock(&port->port.mutex);
  1059. break;
  1060. case TIOCMIWAIT:
  1061. err = wait_msr_change(port, arg);
  1062. break;
  1063. default:
  1064. err = -ENOIOCTLCMD;
  1065. }
  1066. return err;
  1067. }
  1068. static void fwtty_set_termios(struct tty_struct *tty, struct ktermios *old)
  1069. {
  1070. struct fwtty_port *port = tty->driver_data;
  1071. unsigned baud;
  1072. spin_lock_bh(&port->lock);
  1073. baud = set_termios(port, tty);
  1074. if ((baud == 0) && (old->c_cflag & CBAUD)) {
  1075. port->mctrl &= ~(TIOCM_DTR | TIOCM_RTS);
  1076. } else if ((baud != 0) && !(old->c_cflag & CBAUD)) {
  1077. if (C_CRTSCTS(tty) || !test_bit(TTY_THROTTLED, &tty->flags))
  1078. port->mctrl |= TIOCM_DTR | TIOCM_RTS;
  1079. else
  1080. port->mctrl |= TIOCM_DTR;
  1081. }
  1082. __fwtty_write_port_status(port);
  1083. spin_unlock_bh(&port->lock);
  1084. if (old->c_cflag & CRTSCTS) {
  1085. if (!C_CRTSCTS(tty)) {
  1086. tty->hw_stopped = 0;
  1087. fwtty_restart_tx(port);
  1088. }
  1089. } else if (C_CRTSCTS(tty) && ~port->mstatus & TIOCM_CTS) {
  1090. tty->hw_stopped = 1;
  1091. }
  1092. }
  1093. /**
  1094. * fwtty_break_ctl - start/stop sending breaks
  1095. *
  1096. * Signals the remote to start or stop generating simulated breaks.
  1097. * First, stop dequeueing from the fifo and wait for writer/drain to leave tx
  1098. * before signalling the break line status. This guarantees any pending rx will
  1099. * be queued to the line discipline before break is simulated on the remote.
  1100. * Conversely, turning off break_ctl requires signalling the line status change,
  1101. * then enabling tx.
  1102. */
  1103. static int fwtty_break_ctl(struct tty_struct *tty, int state)
  1104. {
  1105. struct fwtty_port *port = tty->driver_data;
  1106. long ret;
  1107. fwtty_dbg(port, "%d\n", state);
  1108. if (state == -1) {
  1109. set_bit(STOP_TX, &port->flags);
  1110. ret = wait_event_interruptible_timeout(port->wait_tx,
  1111. !test_bit(IN_TX, &port->flags),
  1112. 10);
  1113. if (ret == 0 || ret == -ERESTARTSYS) {
  1114. clear_bit(STOP_TX, &port->flags);
  1115. fwtty_restart_tx(port);
  1116. return -EINTR;
  1117. }
  1118. }
  1119. spin_lock_bh(&port->lock);
  1120. port->break_ctl = (state == -1);
  1121. __fwtty_write_port_status(port);
  1122. spin_unlock_bh(&port->lock);
  1123. if (state == 0) {
  1124. spin_lock_bh(&port->lock);
  1125. dma_fifo_reset(&port->tx_fifo);
  1126. clear_bit(STOP_TX, &port->flags);
  1127. spin_unlock_bh(&port->lock);
  1128. }
  1129. return 0;
  1130. }
  1131. static int fwtty_tiocmget(struct tty_struct *tty)
  1132. {
  1133. struct fwtty_port *port = tty->driver_data;
  1134. unsigned tiocm;
  1135. spin_lock_bh(&port->lock);
  1136. tiocm = (port->mctrl & MCTRL_MASK) | (port->mstatus & ~MCTRL_MASK);
  1137. spin_unlock_bh(&port->lock);
  1138. fwtty_dbg(port, "%x\n", tiocm);
  1139. return tiocm;
  1140. }
  1141. static int fwtty_tiocmset(struct tty_struct *tty, unsigned set, unsigned clear)
  1142. {
  1143. struct fwtty_port *port = tty->driver_data;
  1144. fwtty_dbg(port, "set: %x clear: %x\n", set, clear);
  1145. /* TODO: simulate loopback if TIOCM_LOOP set */
  1146. spin_lock_bh(&port->lock);
  1147. port->mctrl &= ~(clear & MCTRL_MASK & 0xffff);
  1148. port->mctrl |= set & MCTRL_MASK & 0xffff;
  1149. __fwtty_write_port_status(port);
  1150. spin_unlock_bh(&port->lock);
  1151. return 0;
  1152. }
  1153. static int fwtty_get_icount(struct tty_struct *tty,
  1154. struct serial_icounter_struct *icount)
  1155. {
  1156. struct fwtty_port *port = tty->driver_data;
  1157. struct stats stats;
  1158. memcpy(&stats, &port->stats, sizeof(stats));
  1159. if (port->port.console)
  1160. (*port->fwcon_ops->stats)(&stats, port->con_data);
  1161. icount->cts = port->icount.cts;
  1162. icount->dsr = port->icount.dsr;
  1163. icount->rng = port->icount.rng;
  1164. icount->dcd = port->icount.dcd;
  1165. icount->rx = port->icount.rx;
  1166. icount->tx = port->icount.tx + stats.xchars;
  1167. icount->frame = port->icount.frame;
  1168. icount->overrun = port->icount.overrun;
  1169. icount->parity = port->icount.parity;
  1170. icount->brk = port->icount.brk;
  1171. icount->buf_overrun = port->icount.overrun;
  1172. return 0;
  1173. }
  1174. static void fwtty_proc_show_port(struct seq_file *m, struct fwtty_port *port)
  1175. {
  1176. struct stats stats;
  1177. memcpy(&stats, &port->stats, sizeof(stats));
  1178. if (port->port.console)
  1179. (*port->fwcon_ops->stats)(&stats, port->con_data);
  1180. seq_printf(m, " addr:%012llx tx:%d rx:%d", port->rx_handler.offset,
  1181. port->icount.tx + stats.xchars, port->icount.rx);
  1182. seq_printf(m, " cts:%d dsr:%d rng:%d dcd:%d", port->icount.cts,
  1183. port->icount.dsr, port->icount.rng, port->icount.dcd);
  1184. seq_printf(m, " fe:%d oe:%d pe:%d brk:%d", port->icount.frame,
  1185. port->icount.overrun, port->icount.parity, port->icount.brk);
  1186. }
  1187. static void fwtty_debugfs_show_port(struct seq_file *m, struct fwtty_port *port)
  1188. {
  1189. struct stats stats;
  1190. memcpy(&stats, &port->stats, sizeof(stats));
  1191. if (port->port.console)
  1192. (*port->fwcon_ops->stats)(&stats, port->con_data);
  1193. seq_printf(m, " dr:%d st:%d err:%d lost:%d", stats.dropped,
  1194. stats.tx_stall, stats.fifo_errs, stats.lost);
  1195. seq_printf(m, " pkts:%d thr:%d", stats.sent, stats.throttled);
  1196. if (port->port.console) {
  1197. seq_puts(m, "\n ");
  1198. (*port->fwcon_ops->proc_show)(m, port->con_data);
  1199. }
  1200. fwtty_dump_profile(m, &port->stats);
  1201. }
  1202. static void fwtty_debugfs_show_peer(struct seq_file *m, struct fwtty_peer *peer)
  1203. {
  1204. int generation = peer->generation;
  1205. smp_rmb();
  1206. seq_printf(m, " %s:", dev_name(&peer->unit->device));
  1207. seq_printf(m, " node:%04x gen:%d", peer->node_id, generation);
  1208. seq_printf(m, " sp:%d max:%d guid:%016llx", peer->speed,
  1209. peer->max_payload, (unsigned long long) peer->guid);
  1210. seq_printf(m, " mgmt:%012llx", (unsigned long long) peer->mgmt_addr);
  1211. seq_printf(m, " addr:%012llx", (unsigned long long) peer->status_addr);
  1212. seq_putc(m, '\n');
  1213. }
  1214. static int fwtty_proc_show(struct seq_file *m, void *v)
  1215. {
  1216. struct fwtty_port *port;
  1217. int i;
  1218. seq_puts(m, "fwserinfo: 1.0 driver: 1.0\n");
  1219. for (i = 0; i < MAX_TOTAL_PORTS && (port = fwtty_port_get(i)); ++i) {
  1220. seq_printf(m, "%2d:", i);
  1221. if (capable(CAP_SYS_ADMIN))
  1222. fwtty_proc_show_port(m, port);
  1223. fwtty_port_put(port);
  1224. seq_puts(m, "\n");
  1225. }
  1226. return 0;
  1227. }
  1228. static int fwtty_debugfs_stats_show(struct seq_file *m, void *v)
  1229. {
  1230. struct fw_serial *serial = m->private;
  1231. struct fwtty_port *port;
  1232. int i;
  1233. for (i = 0; i < num_ports; ++i) {
  1234. port = fwtty_port_get(serial->ports[i]->index);
  1235. if (port) {
  1236. seq_printf(m, "%2d:", port->index);
  1237. fwtty_proc_show_port(m, port);
  1238. fwtty_debugfs_show_port(m, port);
  1239. fwtty_port_put(port);
  1240. seq_puts(m, "\n");
  1241. }
  1242. }
  1243. return 0;
  1244. }
  1245. static int fwtty_debugfs_peers_show(struct seq_file *m, void *v)
  1246. {
  1247. struct fw_serial *serial = m->private;
  1248. struct fwtty_peer *peer;
  1249. rcu_read_lock();
  1250. seq_printf(m, "card: %s guid: %016llx\n",
  1251. dev_name(serial->card->device),
  1252. (unsigned long long) serial->card->guid);
  1253. list_for_each_entry_rcu(peer, &serial->peer_list, list)
  1254. fwtty_debugfs_show_peer(m, peer);
  1255. rcu_read_unlock();
  1256. return 0;
  1257. }
  1258. static int fwtty_proc_open(struct inode *inode, struct file *fp)
  1259. {
  1260. return single_open(fp, fwtty_proc_show, NULL);
  1261. }
  1262. static int fwtty_stats_open(struct inode *inode, struct file *fp)
  1263. {
  1264. return single_open(fp, fwtty_debugfs_stats_show, inode->i_private);
  1265. }
  1266. static int fwtty_peers_open(struct inode *inode, struct file *fp)
  1267. {
  1268. return single_open(fp, fwtty_debugfs_peers_show, inode->i_private);
  1269. }
  1270. static const struct file_operations fwtty_stats_fops = {
  1271. .owner = THIS_MODULE,
  1272. .open = fwtty_stats_open,
  1273. .read = seq_read,
  1274. .llseek = seq_lseek,
  1275. .release = single_release,
  1276. };
  1277. static const struct file_operations fwtty_peers_fops = {
  1278. .owner = THIS_MODULE,
  1279. .open = fwtty_peers_open,
  1280. .read = seq_read,
  1281. .llseek = seq_lseek,
  1282. .release = single_release,
  1283. };
  1284. static const struct file_operations fwtty_proc_fops = {
  1285. .owner = THIS_MODULE,
  1286. .open = fwtty_proc_open,
  1287. .read = seq_read,
  1288. .llseek = seq_lseek,
  1289. .release = single_release,
  1290. };
  1291. static const struct tty_port_operations fwtty_port_ops = {
  1292. .dtr_rts = fwtty_port_dtr_rts,
  1293. .carrier_raised = fwtty_port_carrier_raised,
  1294. .shutdown = fwtty_port_shutdown,
  1295. .activate = fwtty_port_activate,
  1296. };
  1297. static const struct tty_operations fwtty_ops = {
  1298. .open = fwtty_open,
  1299. .close = fwtty_close,
  1300. .hangup = fwtty_hangup,
  1301. .cleanup = fwtty_cleanup,
  1302. .install = fwtty_install,
  1303. .write = fwtty_write,
  1304. .write_room = fwtty_write_room,
  1305. .chars_in_buffer = fwtty_chars_in_buffer,
  1306. .send_xchar = fwtty_send_xchar,
  1307. .throttle = fwtty_throttle,
  1308. .unthrottle = fwtty_unthrottle,
  1309. .ioctl = fwtty_ioctl,
  1310. .set_termios = fwtty_set_termios,
  1311. .break_ctl = fwtty_break_ctl,
  1312. .tiocmget = fwtty_tiocmget,
  1313. .tiocmset = fwtty_tiocmset,
  1314. .get_icount = fwtty_get_icount,
  1315. .proc_fops = &fwtty_proc_fops,
  1316. };
  1317. static const struct tty_operations fwloop_ops = {
  1318. .open = fwtty_open,
  1319. .close = fwtty_close,
  1320. .hangup = fwtty_hangup,
  1321. .cleanup = fwtty_cleanup,
  1322. .install = fwloop_install,
  1323. .write = fwtty_write,
  1324. .write_room = fwtty_write_room,
  1325. .chars_in_buffer = fwtty_chars_in_buffer,
  1326. .send_xchar = fwtty_send_xchar,
  1327. .throttle = fwtty_throttle,
  1328. .unthrottle = fwtty_unthrottle,
  1329. .ioctl = fwtty_ioctl,
  1330. .set_termios = fwtty_set_termios,
  1331. .break_ctl = fwtty_break_ctl,
  1332. .tiocmget = fwtty_tiocmget,
  1333. .tiocmset = fwtty_tiocmset,
  1334. .get_icount = fwtty_get_icount,
  1335. };
  1336. static inline int mgmt_pkt_expected_len(__be16 code)
  1337. {
  1338. static const struct fwserial_mgmt_pkt pkt;
  1339. switch (be16_to_cpu(code)) {
  1340. case FWSC_VIRT_CABLE_PLUG:
  1341. return sizeof(pkt.hdr) + sizeof(pkt.plug_req);
  1342. case FWSC_VIRT_CABLE_PLUG_RSP: /* | FWSC_RSP_OK */
  1343. return sizeof(pkt.hdr) + sizeof(pkt.plug_rsp);
  1344. case FWSC_VIRT_CABLE_UNPLUG:
  1345. case FWSC_VIRT_CABLE_UNPLUG_RSP:
  1346. case FWSC_VIRT_CABLE_PLUG_RSP | FWSC_RSP_NACK:
  1347. case FWSC_VIRT_CABLE_UNPLUG_RSP | FWSC_RSP_NACK:
  1348. return sizeof(pkt.hdr);
  1349. default:
  1350. return -1;
  1351. }
  1352. }
  1353. static inline void fill_plug_params(struct virt_plug_params *params,
  1354. struct fwtty_port *port)
  1355. {
  1356. u64 status_addr = port->rx_handler.offset;
  1357. u64 fifo_addr = port->rx_handler.offset + 4;
  1358. size_t fifo_len = port->rx_handler.length - 4;
  1359. params->status_hi = cpu_to_be32(status_addr >> 32);
  1360. params->status_lo = cpu_to_be32(status_addr);
  1361. params->fifo_hi = cpu_to_be32(fifo_addr >> 32);
  1362. params->fifo_lo = cpu_to_be32(fifo_addr);
  1363. params->fifo_len = cpu_to_be32(fifo_len);
  1364. }
  1365. static inline void fill_plug_req(struct fwserial_mgmt_pkt *pkt,
  1366. struct fwtty_port *port)
  1367. {
  1368. pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG);
  1369. pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
  1370. fill_plug_params(&pkt->plug_req, port);
  1371. }
  1372. static inline void fill_plug_rsp_ok(struct fwserial_mgmt_pkt *pkt,
  1373. struct fwtty_port *port)
  1374. {
  1375. pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP);
  1376. pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
  1377. fill_plug_params(&pkt->plug_rsp, port);
  1378. }
  1379. static inline void fill_plug_rsp_nack(struct fwserial_mgmt_pkt *pkt)
  1380. {
  1381. pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP | FWSC_RSP_NACK);
  1382. pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
  1383. }
  1384. static inline void fill_unplug_req(struct fwserial_mgmt_pkt *pkt)
  1385. {
  1386. pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG);
  1387. pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
  1388. }
  1389. static inline void fill_unplug_rsp_nack(struct fwserial_mgmt_pkt *pkt)
  1390. {
  1391. pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP | FWSC_RSP_NACK);
  1392. pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
  1393. }
  1394. static inline void fill_unplug_rsp_ok(struct fwserial_mgmt_pkt *pkt)
  1395. {
  1396. pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP);
  1397. pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
  1398. }
  1399. static void fwserial_virt_plug_complete(struct fwtty_peer *peer,
  1400. struct virt_plug_params *params)
  1401. {
  1402. struct fwtty_port *port = peer->port;
  1403. peer->status_addr = be32_to_u64(params->status_hi, params->status_lo);
  1404. peer->fifo_addr = be32_to_u64(params->fifo_hi, params->fifo_lo);
  1405. peer->fifo_len = be32_to_cpu(params->fifo_len);
  1406. peer_set_state(peer, FWPS_ATTACHED);
  1407. /* reconfigure tx_fifo optimally for this peer */
  1408. spin_lock_bh(&port->lock);
  1409. port->max_payload = min(peer->max_payload, peer->fifo_len);
  1410. dma_fifo_change_tx_limit(&port->tx_fifo, port->max_payload);
  1411. spin_unlock_bh(&peer->port->lock);
  1412. if (port->port.console && port->fwcon_ops->notify != NULL)
  1413. (*port->fwcon_ops->notify)(FWCON_NOTIFY_ATTACH, port->con_data);
  1414. fwtty_info(&peer->unit, "peer (guid:%016llx) connected on %s\n",
  1415. (unsigned long long)peer->guid, dev_name(port->device));
  1416. }
  1417. static inline int fwserial_send_mgmt_sync(struct fwtty_peer *peer,
  1418. struct fwserial_mgmt_pkt *pkt)
  1419. {
  1420. int generation;
  1421. int rcode, tries = 5;
  1422. do {
  1423. generation = peer->generation;
  1424. smp_rmb();
  1425. rcode = fw_run_transaction(peer->serial->card,
  1426. TCODE_WRITE_BLOCK_REQUEST,
  1427. peer->node_id,
  1428. generation, peer->speed,
  1429. peer->mgmt_addr,
  1430. pkt, be16_to_cpu(pkt->hdr.len));
  1431. if (rcode == RCODE_BUSY || rcode == RCODE_SEND_ERROR ||
  1432. rcode == RCODE_GENERATION) {
  1433. fwtty_dbg(&peer->unit, "mgmt write error: %d\n", rcode);
  1434. continue;
  1435. } else {
  1436. break;
  1437. }
  1438. } while (--tries > 0);
  1439. return rcode;
  1440. }
  1441. /**
  1442. * fwserial_claim_port - attempt to claim port @ index for peer
  1443. *
  1444. * Returns ptr to claimed port or error code (as ERR_PTR())
  1445. * Can sleep - must be called from process context
  1446. */
  1447. static struct fwtty_port *fwserial_claim_port(struct fwtty_peer *peer,
  1448. int index)
  1449. {
  1450. struct fwtty_port *port;
  1451. if (index < 0 || index >= num_ports)
  1452. return ERR_PTR(-EINVAL);
  1453. /* must guarantee that previous port releases have completed */
  1454. synchronize_rcu();
  1455. port = peer->serial->ports[index];
  1456. spin_lock_bh(&port->lock);
  1457. if (!rcu_access_pointer(port->peer))
  1458. rcu_assign_pointer(port->peer, peer);
  1459. else
  1460. port = ERR_PTR(-EBUSY);
  1461. spin_unlock_bh(&port->lock);
  1462. return port;
  1463. }
  1464. /**
  1465. * fwserial_find_port - find avail port and claim for peer
  1466. *
  1467. * Returns ptr to claimed port or NULL if none avail
  1468. * Can sleep - must be called from process context
  1469. */
  1470. static struct fwtty_port *fwserial_find_port(struct fwtty_peer *peer)
  1471. {
  1472. struct fwtty_port **ports = peer->serial->ports;
  1473. int i;
  1474. /* must guarantee that previous port releases have completed */
  1475. synchronize_rcu();
  1476. /* TODO: implement optional GUID-to-specific port # matching */
  1477. /* find an unattached port (but not the loopback port, if present) */
  1478. for (i = 0; i < num_ttys; ++i) {
  1479. spin_lock_bh(&ports[i]->lock);
  1480. if (!ports[i]->peer) {
  1481. /* claim port */
  1482. rcu_assign_pointer(ports[i]->peer, peer);
  1483. spin_unlock_bh(&ports[i]->lock);
  1484. return ports[i];
  1485. }
  1486. spin_unlock_bh(&ports[i]->lock);
  1487. }
  1488. return NULL;
  1489. }
  1490. static void fwserial_release_port(struct fwtty_port *port, bool reset)
  1491. {
  1492. /* drop carrier (and all other line status) */
  1493. if (reset)
  1494. fwtty_update_port_status(port, 0);
  1495. spin_lock_bh(&port->lock);
  1496. /* reset dma fifo max transmission size back to S100 */
  1497. port->max_payload = link_speed_to_max_payload(SCODE_100);
  1498. dma_fifo_change_tx_limit(&port->tx_fifo, port->max_payload);
  1499. RCU_INIT_POINTER(port->peer, NULL);
  1500. spin_unlock_bh(&port->lock);
  1501. if (port->port.console && port->fwcon_ops->notify != NULL)
  1502. (*port->fwcon_ops->notify)(FWCON_NOTIFY_DETACH, port->con_data);
  1503. }
  1504. static void fwserial_plug_timeout(unsigned long data)
  1505. {
  1506. struct fwtty_peer *peer = (struct fwtty_peer *)data;
  1507. struct fwtty_port *port;
  1508. spin_lock_bh(&peer->lock);
  1509. if (peer->state != FWPS_PLUG_PENDING) {
  1510. spin_unlock_bh(&peer->lock);
  1511. return;
  1512. }
  1513. port = peer_revert_state(peer);
  1514. spin_unlock_bh(&peer->lock);
  1515. if (port)
  1516. fwserial_release_port(port, false);
  1517. }
  1518. /**
  1519. * fwserial_connect_peer - initiate virtual cable with peer
  1520. *
  1521. * Returns 0 if VIRT_CABLE_PLUG request was successfully sent,
  1522. * otherwise error code. Must be called from process context.
  1523. */
  1524. static int fwserial_connect_peer(struct fwtty_peer *peer)
  1525. {
  1526. struct fwtty_port *port;
  1527. struct fwserial_mgmt_pkt *pkt;
  1528. int err, rcode;
  1529. pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
  1530. if (!pkt)
  1531. return -ENOMEM;
  1532. port = fwserial_find_port(peer);
  1533. if (!port) {
  1534. fwtty_err(&peer->unit, "avail ports in use\n");
  1535. err = -EBUSY;
  1536. goto free_pkt;
  1537. }
  1538. spin_lock_bh(&peer->lock);
  1539. /* only initiate VIRT_CABLE_PLUG if peer is currently not attached */
  1540. if (peer->state != FWPS_NOT_ATTACHED) {
  1541. err = -EBUSY;
  1542. goto release_port;
  1543. }
  1544. peer->port = port;
  1545. peer_set_state(peer, FWPS_PLUG_PENDING);
  1546. fill_plug_req(pkt, peer->port);
  1547. setup_timer(&peer->timer, fwserial_plug_timeout, (unsigned long)peer);
  1548. mod_timer(&peer->timer, jiffies + VIRT_CABLE_PLUG_TIMEOUT);
  1549. spin_unlock_bh(&peer->lock);
  1550. rcode = fwserial_send_mgmt_sync(peer, pkt);
  1551. spin_lock_bh(&peer->lock);
  1552. if (peer->state == FWPS_PLUG_PENDING && rcode != RCODE_COMPLETE) {
  1553. if (rcode == RCODE_CONFLICT_ERROR)
  1554. err = -EAGAIN;
  1555. else
  1556. err = -EIO;
  1557. goto cancel_timer;
  1558. }
  1559. spin_unlock_bh(&peer->lock);
  1560. kfree(pkt);
  1561. return 0;
  1562. cancel_timer:
  1563. del_timer(&peer->timer);
  1564. peer_revert_state(peer);
  1565. release_port:
  1566. spin_unlock_bh(&peer->lock);
  1567. fwserial_release_port(port, false);
  1568. free_pkt:
  1569. kfree(pkt);
  1570. return err;
  1571. }
  1572. /**
  1573. * fwserial_close_port -
  1574. * HUP the tty (if the tty exists) and unregister the tty device.
  1575. * Only used by the unit driver upon unit removal to disconnect and
  1576. * cleanup all attached ports
  1577. *
  1578. * The port reference is put by fwtty_cleanup (if a reference was
  1579. * ever taken).
  1580. */
  1581. static void fwserial_close_port(struct tty_driver *driver,
  1582. struct fwtty_port *port)
  1583. {
  1584. struct tty_struct *tty;
  1585. mutex_lock(&port->port.mutex);
  1586. tty = tty_port_tty_get(&port->port);
  1587. if (tty) {
  1588. tty_vhangup(tty);
  1589. tty_kref_put(tty);
  1590. }
  1591. mutex_unlock(&port->port.mutex);
  1592. if (driver == fwloop_driver)
  1593. tty_unregister_device(driver, loop_idx(port));
  1594. else
  1595. tty_unregister_device(driver, port->index);
  1596. }
  1597. /**
  1598. * fwserial_lookup - finds first fw_serial associated with card
  1599. * @card: fw_card to match
  1600. *
  1601. * NB: caller must be holding fwserial_list_mutex
  1602. */
  1603. static struct fw_serial *fwserial_lookup(struct fw_card *card)
  1604. {
  1605. struct fw_serial *serial;
  1606. list_for_each_entry(serial, &fwserial_list, list) {
  1607. if (card == serial->card)
  1608. return serial;
  1609. }
  1610. return NULL;
  1611. }
  1612. /**
  1613. * __fwserial_lookup_rcu - finds first fw_serial associated with card
  1614. * @card: fw_card to match
  1615. *
  1616. * NB: caller must be inside rcu_read_lock() section
  1617. */
  1618. static struct fw_serial *__fwserial_lookup_rcu(struct fw_card *card)
  1619. {
  1620. struct fw_serial *serial;
  1621. list_for_each_entry_rcu(serial, &fwserial_list, list) {
  1622. if (card == serial->card)
  1623. return serial;
  1624. }
  1625. return NULL;
  1626. }
  1627. /**
  1628. * __fwserial_peer_by_node_id - finds a peer matching the given generation + id
  1629. *
  1630. * If a matching peer could not be found for the specified generation/node id,
  1631. * this could be because:
  1632. * a) the generation has changed and one of the nodes hasn't updated yet
  1633. * b) the remote node has created its remote unit device before this
  1634. * local node has created its corresponding remote unit device
  1635. * In either case, the remote node should retry
  1636. *
  1637. * Note: caller must be in rcu_read_lock() section
  1638. */
  1639. static struct fwtty_peer *__fwserial_peer_by_node_id(struct fw_card *card,
  1640. int generation, int id)
  1641. {
  1642. struct fw_serial *serial;
  1643. struct fwtty_peer *peer;
  1644. serial = __fwserial_lookup_rcu(card);
  1645. if (!serial) {
  1646. /*
  1647. * Something is very wrong - there should be a matching
  1648. * fw_serial structure for every fw_card. Maybe the remote node
  1649. * has created its remote unit device before this driver has
  1650. * been probed for any unit devices...
  1651. */
  1652. fwtty_err(card, "unknown card (guid %016llx)\n",
  1653. (unsigned long long) card->guid);
  1654. return NULL;
  1655. }
  1656. list_for_each_entry_rcu(peer, &serial->peer_list, list) {
  1657. int g = peer->generation;
  1658. smp_rmb();
  1659. if (generation == g && id == peer->node_id)
  1660. return peer;
  1661. }
  1662. return NULL;
  1663. }
  1664. #ifdef DEBUG
  1665. static void __dump_peer_list(struct fw_card *card)
  1666. {
  1667. struct fw_serial *serial;
  1668. struct fwtty_peer *peer;
  1669. serial = __fwserial_lookup_rcu(card);
  1670. if (!serial)
  1671. return;
  1672. list_for_each_entry_rcu(peer, &serial->peer_list, list) {
  1673. int g = peer->generation;
  1674. smp_rmb();
  1675. fwtty_dbg(card, "peer(%d:%x) guid: %016llx\n",
  1676. g, peer->node_id, (unsigned long long) peer->guid);
  1677. }
  1678. }
  1679. #else
  1680. #define __dump_peer_list(s)
  1681. #endif
  1682. static void fwserial_auto_connect(struct work_struct *work)
  1683. {
  1684. struct fwtty_peer *peer = to_peer(to_delayed_work(work), connect);
  1685. int err;
  1686. err = fwserial_connect_peer(peer);
  1687. if (err == -EAGAIN && ++peer->connect_retries < MAX_CONNECT_RETRIES)
  1688. schedule_delayed_work(&peer->connect, CONNECT_RETRY_DELAY);
  1689. }
  1690. static void fwserial_peer_workfn(struct work_struct *work)
  1691. {
  1692. struct fwtty_peer *peer = to_peer(work, work);
  1693. peer->workfn(work);
  1694. }
  1695. /**
  1696. * fwserial_add_peer - add a newly probed 'serial' unit device as a 'peer'
  1697. * @serial: aggregate representing the specific fw_card to add the peer to
  1698. * @unit: 'peer' to create and add to peer_list of serial
  1699. *
  1700. * Adds a 'peer' (ie, a local or remote 'serial' unit device) to the list of
  1701. * peers for a specific fw_card. Optionally, auto-attach this peer to an
  1702. * available tty port. This function is called either directly or indirectly
  1703. * as a result of a 'serial' unit device being created & probed.
  1704. *
  1705. * Note: this function is serialized with fwserial_remove_peer() by the
  1706. * fwserial_list_mutex held in fwserial_probe().
  1707. *
  1708. * A 1:1 correspondence between an fw_unit and an fwtty_peer is maintained
  1709. * via the dev_set_drvdata() for the device of the fw_unit.
  1710. */
  1711. static int fwserial_add_peer(struct fw_serial *serial, struct fw_unit *unit)
  1712. {
  1713. struct device *dev = &unit->device;
  1714. struct fw_device *parent = fw_parent_device(unit);
  1715. struct fwtty_peer *peer;
  1716. struct fw_csr_iterator ci;
  1717. int key, val;
  1718. int generation;
  1719. peer = kzalloc(sizeof(*peer), GFP_KERNEL);
  1720. if (!peer)
  1721. return -ENOMEM;
  1722. peer_set_state(peer, FWPS_NOT_ATTACHED);
  1723. dev_set_drvdata(dev, peer);
  1724. peer->unit = unit;
  1725. peer->guid = (u64)parent->config_rom[3] << 32 | parent->config_rom[4];
  1726. peer->speed = parent->max_speed;
  1727. peer->max_payload = min(device_max_receive(parent),
  1728. link_speed_to_max_payload(peer->speed));
  1729. generation = parent->generation;
  1730. smp_rmb();
  1731. peer->node_id = parent->node_id;
  1732. smp_wmb();
  1733. peer->generation = generation;
  1734. /* retrieve the mgmt bus addr from the unit directory */
  1735. fw_csr_iterator_init(&ci, unit->directory);
  1736. while (fw_csr_iterator_next(&ci, &key, &val)) {
  1737. if (key == (CSR_OFFSET | CSR_DEPENDENT_INFO)) {
  1738. peer->mgmt_addr = CSR_REGISTER_BASE + 4 * val;
  1739. break;
  1740. }
  1741. }
  1742. if (peer->mgmt_addr == 0ULL) {
  1743. /*
  1744. * No mgmt address effectively disables VIRT_CABLE_PLUG -
  1745. * this peer will not be able to attach to a remote
  1746. */
  1747. peer_set_state(peer, FWPS_NO_MGMT_ADDR);
  1748. }
  1749. spin_lock_init(&peer->lock);
  1750. peer->port = NULL;
  1751. init_timer(&peer->timer);
  1752. INIT_WORK(&peer->work, fwserial_peer_workfn);
  1753. INIT_DELAYED_WORK(&peer->connect, fwserial_auto_connect);
  1754. /* associate peer with specific fw_card */
  1755. peer->serial = serial;
  1756. list_add_rcu(&peer->list, &serial->peer_list);
  1757. fwtty_info(&peer->unit, "peer added (guid:%016llx)\n",
  1758. (unsigned long long)peer->guid);
  1759. /* identify the local unit & virt cable to loopback port */
  1760. if (parent->is_local) {
  1761. serial->self = peer;
  1762. if (create_loop_dev) {
  1763. struct fwtty_port *port;
  1764. port = fwserial_claim_port(peer, num_ttys);
  1765. if (!IS_ERR(port)) {
  1766. struct virt_plug_params params;
  1767. spin_lock_bh(&peer->lock);
  1768. peer->port = port;
  1769. fill_plug_params(&params, port);
  1770. fwserial_virt_plug_complete(peer, &params);
  1771. spin_unlock_bh(&peer->lock);
  1772. fwtty_write_port_status(port);
  1773. }
  1774. }
  1775. } else if (auto_connect) {
  1776. /* auto-attach to remote units only (if policy allows) */
  1777. schedule_delayed_work(&peer->connect, 1);
  1778. }
  1779. return 0;
  1780. }
  1781. /**
  1782. * fwserial_remove_peer - remove a 'serial' unit device as a 'peer'
  1783. *
  1784. * Remove a 'peer' from its list of peers. This function is only
  1785. * called by fwserial_remove() on bus removal of the unit device.
  1786. *
  1787. * Note: this function is serialized with fwserial_add_peer() by the
  1788. * fwserial_list_mutex held in fwserial_remove().
  1789. */
  1790. static void fwserial_remove_peer(struct fwtty_peer *peer)
  1791. {
  1792. struct fwtty_port *port;
  1793. spin_lock_bh(&peer->lock);
  1794. peer_set_state(peer, FWPS_GONE);
  1795. spin_unlock_bh(&peer->lock);
  1796. cancel_delayed_work_sync(&peer->connect);
  1797. cancel_work_sync(&peer->work);
  1798. spin_lock_bh(&peer->lock);
  1799. /* if this unit is the local unit, clear link */
  1800. if (peer == peer->serial->self)
  1801. peer->serial->self = NULL;
  1802. /* cancel the request timeout timer (if running) */
  1803. del_timer(&peer->timer);
  1804. port = peer->port;
  1805. peer->port = NULL;
  1806. list_del_rcu(&peer->list);
  1807. fwtty_info(&peer->unit, "peer removed (guid:%016llx)\n",
  1808. (unsigned long long)peer->guid);
  1809. spin_unlock_bh(&peer->lock);
  1810. if (port)
  1811. fwserial_release_port(port, true);
  1812. synchronize_rcu();
  1813. kfree(peer);
  1814. }
  1815. /**
  1816. * fwserial_create - init everything to create TTYs for a specific fw_card
  1817. * @unit: fw_unit for first 'serial' unit device probed for this fw_card
  1818. *
  1819. * This function inits the aggregate structure (an fw_serial instance)
  1820. * used to manage the TTY ports registered by a specific fw_card. Also, the
  1821. * unit device is added as the first 'peer'.
  1822. *
  1823. * This unit device may represent a local unit device (as specified by the
  1824. * config ROM unit directory) or it may represent a remote unit device
  1825. * (as specified by the reading of the remote node's config ROM).
  1826. *
  1827. * Returns 0 to indicate "ownership" of the unit device, or a negative errno
  1828. * value to indicate which error.
  1829. */
  1830. static int fwserial_create(struct fw_unit *unit)
  1831. {
  1832. struct fw_device *parent = fw_parent_device(unit);
  1833. struct fw_card *card = parent->card;
  1834. struct fw_serial *serial;
  1835. struct fwtty_port *port;
  1836. struct device *tty_dev;
  1837. int i, j;
  1838. int err;
  1839. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  1840. if (!serial)
  1841. return -ENOMEM;
  1842. kref_init(&serial->kref);
  1843. serial->card = card;
  1844. INIT_LIST_HEAD(&serial->peer_list);
  1845. for (i = 0; i < num_ports; ++i) {
  1846. port = kzalloc(sizeof(*port), GFP_KERNEL);
  1847. if (!port) {
  1848. err = -ENOMEM;
  1849. goto free_ports;
  1850. }
  1851. tty_port_init(&port->port);
  1852. port->index = FWTTY_INVALID_INDEX;
  1853. port->port.ops = &fwtty_port_ops;
  1854. port->serial = serial;
  1855. tty_buffer_set_limit(&port->port, 128 * 1024);
  1856. spin_lock_init(&port->lock);
  1857. INIT_DELAYED_WORK(&port->drain, fwtty_drain_tx);
  1858. INIT_DELAYED_WORK(&port->emit_breaks, fwtty_emit_breaks);
  1859. INIT_WORK(&port->hangup, fwtty_do_hangup);
  1860. init_waitqueue_head(&port->wait_tx);
  1861. port->max_payload = link_speed_to_max_payload(SCODE_100);
  1862. dma_fifo_init(&port->tx_fifo);
  1863. RCU_INIT_POINTER(port->peer, NULL);
  1864. serial->ports[i] = port;
  1865. /* get unique bus addr region for port's status & recv fifo */
  1866. port->rx_handler.length = FWTTY_PORT_RXFIFO_LEN + 4;
  1867. port->rx_handler.address_callback = fwtty_port_handler;
  1868. port->rx_handler.callback_data = port;
  1869. /*
  1870. * XXX: use custom memory region above cpu physical memory addrs
  1871. * this will ease porting to 64-bit firewire adapters
  1872. */
  1873. err = fw_core_add_address_handler(&port->rx_handler,
  1874. &fw_high_memory_region);
  1875. if (err) {
  1876. kfree(port);
  1877. goto free_ports;
  1878. }
  1879. }
  1880. /* preserve i for error cleanup */
  1881. err = fwtty_ports_add(serial);
  1882. if (err) {
  1883. fwtty_err(&unit, "no space in port table\n");
  1884. goto free_ports;
  1885. }
  1886. for (j = 0; j < num_ttys; ++j) {
  1887. tty_dev = tty_port_register_device(&serial->ports[j]->port,
  1888. fwtty_driver,
  1889. serial->ports[j]->index,
  1890. card->device);
  1891. if (IS_ERR(tty_dev)) {
  1892. err = PTR_ERR(tty_dev);
  1893. fwtty_err(&unit, "register tty device error (%d)\n",
  1894. err);
  1895. goto unregister_ttys;
  1896. }
  1897. serial->ports[j]->device = tty_dev;
  1898. }
  1899. /* preserve j for error cleanup */
  1900. if (create_loop_dev) {
  1901. struct device *loop_dev;
  1902. loop_dev = tty_port_register_device(&serial->ports[j]->port,
  1903. fwloop_driver,
  1904. loop_idx(serial->ports[j]),
  1905. card->device);
  1906. if (IS_ERR(loop_dev)) {
  1907. err = PTR_ERR(loop_dev);
  1908. fwtty_err(&unit, "create loop device failed (%d)\n",
  1909. err);
  1910. goto unregister_ttys;
  1911. }
  1912. serial->ports[j]->device = loop_dev;
  1913. serial->ports[j]->loopback = true;
  1914. }
  1915. if (!IS_ERR_OR_NULL(fwserial_debugfs)) {
  1916. serial->debugfs = debugfs_create_dir(dev_name(&unit->device),
  1917. fwserial_debugfs);
  1918. if (!IS_ERR_OR_NULL(serial->debugfs)) {
  1919. debugfs_create_file("peers", 0444, serial->debugfs,
  1920. serial, &fwtty_peers_fops);
  1921. debugfs_create_file("stats", 0444, serial->debugfs,
  1922. serial, &fwtty_stats_fops);
  1923. }
  1924. }
  1925. list_add_rcu(&serial->list, &fwserial_list);
  1926. fwtty_notice(&unit, "TTY over FireWire on device %s (guid %016llx)\n",
  1927. dev_name(card->device), (unsigned long long) card->guid);
  1928. err = fwserial_add_peer(serial, unit);
  1929. if (!err)
  1930. return 0;
  1931. fwtty_err(&unit, "unable to add peer unit device (%d)\n", err);
  1932. /* fall-through to error processing */
  1933. debugfs_remove_recursive(serial->debugfs);
  1934. list_del_rcu(&serial->list);
  1935. if (create_loop_dev)
  1936. tty_unregister_device(fwloop_driver,
  1937. loop_idx(serial->ports[j]));
  1938. unregister_ttys:
  1939. for (--j; j >= 0; --j)
  1940. tty_unregister_device(fwtty_driver, serial->ports[j]->index);
  1941. kref_put(&serial->kref, fwserial_destroy);
  1942. return err;
  1943. free_ports:
  1944. for (--i; i >= 0; --i) {
  1945. tty_port_destroy(&serial->ports[i]->port);
  1946. kfree(serial->ports[i]);
  1947. }
  1948. kfree(serial);
  1949. return err;
  1950. }
  1951. /**
  1952. * fwserial_probe: bus probe function for firewire 'serial' unit devices
  1953. *
  1954. * A 'serial' unit device is created and probed as a result of:
  1955. * - declaring a ieee1394 bus id table for 'devices' matching a fabricated
  1956. * 'serial' unit specifier id
  1957. * - adding a unit directory to the config ROM(s) for a 'serial' unit
  1958. *
  1959. * The firewire core registers unit devices by enumerating unit directories
  1960. * of a node's config ROM after reading the config ROM when a new node is
  1961. * added to the bus topology after a bus reset.
  1962. *
  1963. * The practical implications of this are:
  1964. * - this probe is called for both local and remote nodes that have a 'serial'
  1965. * unit directory in their config ROM (that matches the specifiers in
  1966. * fwserial_id_table).
  1967. * - no specific order is enforced for local vs. remote unit devices
  1968. *
  1969. * This unit driver copes with the lack of specific order in the same way the
  1970. * firewire net driver does -- each probe, for either a local or remote unit
  1971. * device, is treated as a 'peer' (has a struct fwtty_peer instance) and the
  1972. * first peer created for a given fw_card (tracked by the global fwserial_list)
  1973. * creates the underlying TTYs (aggregated in a fw_serial instance).
  1974. *
  1975. * NB: an early attempt to differentiate local & remote unit devices by creating
  1976. * peers only for remote units and fw_serial instances (with their
  1977. * associated TTY devices) only for local units was discarded. Managing
  1978. * the peer lifetimes on device removal proved too complicated.
  1979. *
  1980. * fwserial_probe/fwserial_remove are effectively serialized by the
  1981. * fwserial_list_mutex. This is necessary because the addition of the first peer
  1982. * for a given fw_card will trigger the creation of the fw_serial for that
  1983. * fw_card, which must not simultaneously contend with the removal of the
  1984. * last peer for a given fw_card triggering the destruction of the same
  1985. * fw_serial for the same fw_card.
  1986. */
  1987. static int fwserial_probe(struct fw_unit *unit,
  1988. const struct ieee1394_device_id *id)
  1989. {
  1990. struct fw_serial *serial;
  1991. int err;
  1992. mutex_lock(&fwserial_list_mutex);
  1993. serial = fwserial_lookup(fw_parent_device(unit)->card);
  1994. if (!serial)
  1995. err = fwserial_create(unit);
  1996. else
  1997. err = fwserial_add_peer(serial, unit);
  1998. mutex_unlock(&fwserial_list_mutex);
  1999. return err;
  2000. }
  2001. /**
  2002. * fwserial_remove: bus removal function for firewire 'serial' unit devices
  2003. *
  2004. * The corresponding 'peer' for this unit device is removed from the list of
  2005. * peers for the associated fw_serial (which has a 1:1 correspondence with a
  2006. * specific fw_card). If this is the last peer being removed, then trigger
  2007. * the destruction of the underlying TTYs.
  2008. */
  2009. static void fwserial_remove(struct fw_unit *unit)
  2010. {
  2011. struct fwtty_peer *peer = dev_get_drvdata(&unit->device);
  2012. struct fw_serial *serial = peer->serial;
  2013. int i;
  2014. mutex_lock(&fwserial_list_mutex);
  2015. fwserial_remove_peer(peer);
  2016. if (list_empty(&serial->peer_list)) {
  2017. /* unlink from the fwserial_list here */
  2018. list_del_rcu(&serial->list);
  2019. debugfs_remove_recursive(serial->debugfs);
  2020. for (i = 0; i < num_ttys; ++i)
  2021. fwserial_close_port(fwtty_driver, serial->ports[i]);
  2022. if (create_loop_dev)
  2023. fwserial_close_port(fwloop_driver, serial->ports[i]);
  2024. kref_put(&serial->kref, fwserial_destroy);
  2025. }
  2026. mutex_unlock(&fwserial_list_mutex);
  2027. }
  2028. /**
  2029. * fwserial_update: bus update function for 'firewire' serial unit devices
  2030. *
  2031. * Updates the new node_id and bus generation for this peer. Note that locking
  2032. * is unnecessary; but careful memory barrier usage is important to enforce the
  2033. * load and store order of generation & node_id.
  2034. *
  2035. * The fw-core orders the write of node_id before generation in the parent
  2036. * fw_device to ensure that a stale node_id cannot be used with a current
  2037. * bus generation. So the generation value must be read before the node_id.
  2038. *
  2039. * In turn, this orders the write of node_id before generation in the peer to
  2040. * also ensure a stale node_id cannot be used with a current bus generation.
  2041. */
  2042. static void fwserial_update(struct fw_unit *unit)
  2043. {
  2044. struct fw_device *parent = fw_parent_device(unit);
  2045. struct fwtty_peer *peer = dev_get_drvdata(&unit->device);
  2046. int generation;
  2047. generation = parent->generation;
  2048. smp_rmb();
  2049. peer->node_id = parent->node_id;
  2050. smp_wmb();
  2051. peer->generation = generation;
  2052. }
  2053. static const struct ieee1394_device_id fwserial_id_table[] = {
  2054. {
  2055. .match_flags = IEEE1394_MATCH_SPECIFIER_ID |
  2056. IEEE1394_MATCH_VERSION,
  2057. .specifier_id = LINUX_VENDOR_ID,
  2058. .version = FWSERIAL_VERSION,
  2059. },
  2060. { }
  2061. };
  2062. static struct fw_driver fwserial_driver = {
  2063. .driver = {
  2064. .owner = THIS_MODULE,
  2065. .name = KBUILD_MODNAME,
  2066. .bus = &fw_bus_type,
  2067. },
  2068. .probe = fwserial_probe,
  2069. .update = fwserial_update,
  2070. .remove = fwserial_remove,
  2071. .id_table = fwserial_id_table,
  2072. };
  2073. #define FW_UNIT_SPECIFIER(id) ((CSR_SPECIFIER_ID << 24) | (id))
  2074. #define FW_UNIT_VERSION(ver) ((CSR_VERSION << 24) | (ver))
  2075. #define FW_UNIT_ADDRESS(ofs) (((CSR_OFFSET | CSR_DEPENDENT_INFO) << 24) \
  2076. | (((ofs) - CSR_REGISTER_BASE) >> 2))
  2077. /* XXX: config ROM definitons could be improved with semi-automated offset
  2078. * and length calculation
  2079. */
  2080. #define FW_ROM_LEN(quads) ((quads) << 16)
  2081. #define FW_ROM_DESCRIPTOR(ofs) (((CSR_LEAF | CSR_DESCRIPTOR) << 24) | (ofs))
  2082. struct fwserial_unit_directory_data {
  2083. u32 len_crc;
  2084. u32 unit_specifier;
  2085. u32 unit_sw_version;
  2086. u32 unit_addr_offset;
  2087. u32 desc1_ofs;
  2088. u32 desc1_len_crc;
  2089. u32 desc1_data[5];
  2090. } __packed;
  2091. static struct fwserial_unit_directory_data fwserial_unit_directory_data = {
  2092. .len_crc = FW_ROM_LEN(4),
  2093. .unit_specifier = FW_UNIT_SPECIFIER(LINUX_VENDOR_ID),
  2094. .unit_sw_version = FW_UNIT_VERSION(FWSERIAL_VERSION),
  2095. .desc1_ofs = FW_ROM_DESCRIPTOR(1),
  2096. .desc1_len_crc = FW_ROM_LEN(5),
  2097. .desc1_data = {
  2098. 0x00000000, /* type = text */
  2099. 0x00000000, /* enc = ASCII, lang EN */
  2100. 0x4c696e75, /* 'Linux TTY' */
  2101. 0x78205454,
  2102. 0x59000000,
  2103. },
  2104. };
  2105. static struct fw_descriptor fwserial_unit_directory = {
  2106. .length = sizeof(fwserial_unit_directory_data) / sizeof(u32),
  2107. .key = (CSR_DIRECTORY | CSR_UNIT) << 24,
  2108. .data = (u32 *)&fwserial_unit_directory_data,
  2109. };
  2110. /*
  2111. * The management address is in the unit space region but above other known
  2112. * address users (to keep wild writes from causing havoc)
  2113. */
  2114. static const struct fw_address_region fwserial_mgmt_addr_region = {
  2115. .start = CSR_REGISTER_BASE + 0x1e0000ULL,
  2116. .end = 0x1000000000000ULL,
  2117. };
  2118. static struct fw_address_handler fwserial_mgmt_addr_handler;
  2119. /**
  2120. * fwserial_handle_plug_req - handle VIRT_CABLE_PLUG request work
  2121. * @work: ptr to peer->work
  2122. *
  2123. * Attempts to complete the VIRT_CABLE_PLUG handshake sequence for this peer.
  2124. *
  2125. * This checks for a collided request-- ie, that a VIRT_CABLE_PLUG request was
  2126. * already sent to this peer. If so, the collision is resolved by comparing
  2127. * guid values; the loser sends the plug response.
  2128. *
  2129. * Note: if an error prevents a response, don't do anything -- the
  2130. * remote will timeout its request.
  2131. */
  2132. static void fwserial_handle_plug_req(struct work_struct *work)
  2133. {
  2134. struct fwtty_peer *peer = to_peer(work, work);
  2135. struct virt_plug_params *plug_req = &peer->work_params.plug_req;
  2136. struct fwtty_port *port;
  2137. struct fwserial_mgmt_pkt *pkt;
  2138. int rcode;
  2139. pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
  2140. if (!pkt)
  2141. return;
  2142. port = fwserial_find_port(peer);
  2143. spin_lock_bh(&peer->lock);
  2144. switch (peer->state) {
  2145. case FWPS_NOT_ATTACHED:
  2146. if (!port) {
  2147. fwtty_err(&peer->unit, "no more ports avail\n");
  2148. fill_plug_rsp_nack(pkt);
  2149. } else {
  2150. peer->port = port;
  2151. fill_plug_rsp_ok(pkt, peer->port);
  2152. peer_set_state(peer, FWPS_PLUG_RESPONDING);
  2153. /* don't release claimed port */
  2154. port = NULL;
  2155. }
  2156. break;
  2157. case FWPS_PLUG_PENDING:
  2158. if (peer->serial->card->guid > peer->guid)
  2159. goto cleanup;
  2160. /* We lost - hijack the already-claimed port and send ok */
  2161. del_timer(&peer->timer);
  2162. fill_plug_rsp_ok(pkt, peer->port);
  2163. peer_set_state(peer, FWPS_PLUG_RESPONDING);
  2164. break;
  2165. default:
  2166. fill_plug_rsp_nack(pkt);
  2167. }
  2168. spin_unlock_bh(&peer->lock);
  2169. if (port)
  2170. fwserial_release_port(port, false);
  2171. rcode = fwserial_send_mgmt_sync(peer, pkt);
  2172. spin_lock_bh(&peer->lock);
  2173. if (peer->state == FWPS_PLUG_RESPONDING) {
  2174. if (rcode == RCODE_COMPLETE) {
  2175. struct fwtty_port *tmp = peer->port;
  2176. fwserial_virt_plug_complete(peer, plug_req);
  2177. spin_unlock_bh(&peer->lock);
  2178. fwtty_write_port_status(tmp);
  2179. spin_lock_bh(&peer->lock);
  2180. } else {
  2181. fwtty_err(&peer->unit, "PLUG_RSP error (%d)\n", rcode);
  2182. port = peer_revert_state(peer);
  2183. }
  2184. }
  2185. cleanup:
  2186. spin_unlock_bh(&peer->lock);
  2187. if (port)
  2188. fwserial_release_port(port, false);
  2189. kfree(pkt);
  2190. }
  2191. static void fwserial_handle_unplug_req(struct work_struct *work)
  2192. {
  2193. struct fwtty_peer *peer = to_peer(work, work);
  2194. struct fwtty_port *port = NULL;
  2195. struct fwserial_mgmt_pkt *pkt;
  2196. int rcode;
  2197. pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
  2198. if (!pkt)
  2199. return;
  2200. spin_lock_bh(&peer->lock);
  2201. switch (peer->state) {
  2202. case FWPS_ATTACHED:
  2203. fill_unplug_rsp_ok(pkt);
  2204. peer_set_state(peer, FWPS_UNPLUG_RESPONDING);
  2205. break;
  2206. case FWPS_UNPLUG_PENDING:
  2207. if (peer->serial->card->guid > peer->guid)
  2208. goto cleanup;
  2209. /* We lost - send unplug rsp */
  2210. del_timer(&peer->timer);
  2211. fill_unplug_rsp_ok(pkt);
  2212. peer_set_state(peer, FWPS_UNPLUG_RESPONDING);
  2213. break;
  2214. default:
  2215. fill_unplug_rsp_nack(pkt);
  2216. }
  2217. spin_unlock_bh(&peer->lock);
  2218. rcode = fwserial_send_mgmt_sync(peer, pkt);
  2219. spin_lock_bh(&peer->lock);
  2220. if (peer->state == FWPS_UNPLUG_RESPONDING) {
  2221. if (rcode != RCODE_COMPLETE)
  2222. fwtty_err(&peer->unit, "UNPLUG_RSP error (%d)\n",
  2223. rcode);
  2224. port = peer_revert_state(peer);
  2225. }
  2226. cleanup:
  2227. spin_unlock_bh(&peer->lock);
  2228. if (port)
  2229. fwserial_release_port(port, true);
  2230. kfree(pkt);
  2231. }
  2232. static int fwserial_parse_mgmt_write(struct fwtty_peer *peer,
  2233. struct fwserial_mgmt_pkt *pkt,
  2234. unsigned long long addr,
  2235. size_t len)
  2236. {
  2237. struct fwtty_port *port = NULL;
  2238. bool reset = false;
  2239. int rcode;
  2240. if (addr != fwserial_mgmt_addr_handler.offset || len < sizeof(pkt->hdr))
  2241. return RCODE_ADDRESS_ERROR;
  2242. if (len != be16_to_cpu(pkt->hdr.len) ||
  2243. len != mgmt_pkt_expected_len(pkt->hdr.code))
  2244. return RCODE_DATA_ERROR;
  2245. spin_lock_bh(&peer->lock);
  2246. if (peer->state == FWPS_GONE) {
  2247. /*
  2248. * This should never happen - it would mean that the
  2249. * remote unit that just wrote this transaction was
  2250. * already removed from the bus -- and the removal was
  2251. * processed before we rec'd this transaction
  2252. */
  2253. fwtty_err(&peer->unit, "peer already removed\n");
  2254. spin_unlock_bh(&peer->lock);
  2255. return RCODE_ADDRESS_ERROR;
  2256. }
  2257. rcode = RCODE_COMPLETE;
  2258. fwtty_dbg(&peer->unit, "mgmt: hdr.code: %04hx\n", pkt->hdr.code);
  2259. switch (be16_to_cpu(pkt->hdr.code) & FWSC_CODE_MASK) {
  2260. case FWSC_VIRT_CABLE_PLUG:
  2261. if (work_pending(&peer->work)) {
  2262. fwtty_err(&peer->unit, "plug req: busy\n");
  2263. rcode = RCODE_CONFLICT_ERROR;
  2264. } else {
  2265. peer->work_params.plug_req = pkt->plug_req;
  2266. peer->workfn = fwserial_handle_plug_req;
  2267. queue_work(system_unbound_wq, &peer->work);
  2268. }
  2269. break;
  2270. case FWSC_VIRT_CABLE_PLUG_RSP:
  2271. if (peer->state != FWPS_PLUG_PENDING) {
  2272. rcode = RCODE_CONFLICT_ERROR;
  2273. } else if (be16_to_cpu(pkt->hdr.code) & FWSC_RSP_NACK) {
  2274. fwtty_notice(&peer->unit, "NACK plug rsp\n");
  2275. port = peer_revert_state(peer);
  2276. } else {
  2277. struct fwtty_port *tmp = peer->port;
  2278. fwserial_virt_plug_complete(peer, &pkt->plug_rsp);
  2279. spin_unlock_bh(&peer->lock);
  2280. fwtty_write_port_status(tmp);
  2281. spin_lock_bh(&peer->lock);
  2282. }
  2283. break;
  2284. case FWSC_VIRT_CABLE_UNPLUG:
  2285. if (work_pending(&peer->work)) {
  2286. fwtty_err(&peer->unit, "unplug req: busy\n");
  2287. rcode = RCODE_CONFLICT_ERROR;
  2288. } else {
  2289. peer->workfn = fwserial_handle_unplug_req;
  2290. queue_work(system_unbound_wq, &peer->work);
  2291. }
  2292. break;
  2293. case FWSC_VIRT_CABLE_UNPLUG_RSP:
  2294. if (peer->state != FWPS_UNPLUG_PENDING) {
  2295. rcode = RCODE_CONFLICT_ERROR;
  2296. } else {
  2297. if (be16_to_cpu(pkt->hdr.code) & FWSC_RSP_NACK)
  2298. fwtty_notice(&peer->unit, "NACK unplug?\n");
  2299. port = peer_revert_state(peer);
  2300. reset = true;
  2301. }
  2302. break;
  2303. default:
  2304. fwtty_err(&peer->unit, "unknown mgmt code %d\n",
  2305. be16_to_cpu(pkt->hdr.code));
  2306. rcode = RCODE_DATA_ERROR;
  2307. }
  2308. spin_unlock_bh(&peer->lock);
  2309. if (port)
  2310. fwserial_release_port(port, reset);
  2311. return rcode;
  2312. }
  2313. /**
  2314. * fwserial_mgmt_handler: bus address handler for mgmt requests
  2315. * @parameters: fw_address_callback_t as specified by firewire core interface
  2316. *
  2317. * This handler is responsible for handling virtual cable requests from remotes
  2318. * for all cards.
  2319. */
  2320. static void fwserial_mgmt_handler(struct fw_card *card,
  2321. struct fw_request *request,
  2322. int tcode, int destination, int source,
  2323. int generation,
  2324. unsigned long long addr,
  2325. void *data, size_t len,
  2326. void *callback_data)
  2327. {
  2328. struct fwserial_mgmt_pkt *pkt = data;
  2329. struct fwtty_peer *peer;
  2330. int rcode;
  2331. rcu_read_lock();
  2332. peer = __fwserial_peer_by_node_id(card, generation, source);
  2333. if (!peer) {
  2334. fwtty_dbg(card, "peer(%d:%x) not found\n", generation, source);
  2335. __dump_peer_list(card);
  2336. rcode = RCODE_CONFLICT_ERROR;
  2337. } else {
  2338. switch (tcode) {
  2339. case TCODE_WRITE_BLOCK_REQUEST:
  2340. rcode = fwserial_parse_mgmt_write(peer, pkt, addr, len);
  2341. break;
  2342. default:
  2343. rcode = RCODE_TYPE_ERROR;
  2344. }
  2345. }
  2346. rcu_read_unlock();
  2347. fw_send_response(card, request, rcode);
  2348. }
  2349. static int __init fwserial_init(void)
  2350. {
  2351. int err, num_loops = !!(create_loop_dev);
  2352. /* XXX: placeholder for a "firewire" debugfs node */
  2353. fwserial_debugfs = debugfs_create_dir(KBUILD_MODNAME, NULL);
  2354. /* num_ttys/num_ports must not be set above the static alloc avail */
  2355. if (num_ttys + num_loops > MAX_CARD_PORTS)
  2356. num_ttys = MAX_CARD_PORTS - num_loops;
  2357. num_ports = num_ttys + num_loops;
  2358. fwtty_driver = tty_alloc_driver(MAX_TOTAL_PORTS, TTY_DRIVER_REAL_RAW
  2359. | TTY_DRIVER_DYNAMIC_DEV);
  2360. if (IS_ERR(fwtty_driver)) {
  2361. err = PTR_ERR(fwtty_driver);
  2362. goto remove_debugfs;
  2363. }
  2364. fwtty_driver->driver_name = KBUILD_MODNAME;
  2365. fwtty_driver->name = tty_dev_name;
  2366. fwtty_driver->major = 0;
  2367. fwtty_driver->minor_start = 0;
  2368. fwtty_driver->type = TTY_DRIVER_TYPE_SERIAL;
  2369. fwtty_driver->subtype = SERIAL_TYPE_NORMAL;
  2370. fwtty_driver->init_termios = tty_std_termios;
  2371. fwtty_driver->init_termios.c_cflag |= CLOCAL;
  2372. tty_set_operations(fwtty_driver, &fwtty_ops);
  2373. err = tty_register_driver(fwtty_driver);
  2374. if (err) {
  2375. pr_err("register tty driver failed (%d)\n", err);
  2376. goto put_tty;
  2377. }
  2378. if (create_loop_dev) {
  2379. fwloop_driver = tty_alloc_driver(MAX_TOTAL_PORTS / num_ports,
  2380. TTY_DRIVER_REAL_RAW
  2381. | TTY_DRIVER_DYNAMIC_DEV);
  2382. if (IS_ERR(fwloop_driver)) {
  2383. err = PTR_ERR(fwloop_driver);
  2384. goto unregister_driver;
  2385. }
  2386. fwloop_driver->driver_name = KBUILD_MODNAME "_loop";
  2387. fwloop_driver->name = loop_dev_name;
  2388. fwloop_driver->major = 0;
  2389. fwloop_driver->minor_start = 0;
  2390. fwloop_driver->type = TTY_DRIVER_TYPE_SERIAL;
  2391. fwloop_driver->subtype = SERIAL_TYPE_NORMAL;
  2392. fwloop_driver->init_termios = tty_std_termios;
  2393. fwloop_driver->init_termios.c_cflag |= CLOCAL;
  2394. tty_set_operations(fwloop_driver, &fwloop_ops);
  2395. err = tty_register_driver(fwloop_driver);
  2396. if (err) {
  2397. pr_err("register loop driver failed (%d)\n", err);
  2398. goto put_loop;
  2399. }
  2400. }
  2401. fwtty_txn_cache = kmem_cache_create("fwtty_txn_cache",
  2402. sizeof(struct fwtty_transaction),
  2403. 0, 0, fwtty_txn_constructor);
  2404. if (!fwtty_txn_cache) {
  2405. err = -ENOMEM;
  2406. goto unregister_loop;
  2407. }
  2408. /*
  2409. * Ideally, this address handler would be registered per local node
  2410. * (rather than the same handler for all local nodes). However,
  2411. * since the firewire core requires the config rom descriptor *before*
  2412. * the local unit device(s) are created, a single management handler
  2413. * must suffice for all local serial units.
  2414. */
  2415. fwserial_mgmt_addr_handler.length = sizeof(struct fwserial_mgmt_pkt);
  2416. fwserial_mgmt_addr_handler.address_callback = fwserial_mgmt_handler;
  2417. err = fw_core_add_address_handler(&fwserial_mgmt_addr_handler,
  2418. &fwserial_mgmt_addr_region);
  2419. if (err) {
  2420. pr_err("add management handler failed (%d)\n", err);
  2421. goto destroy_cache;
  2422. }
  2423. fwserial_unit_directory_data.unit_addr_offset =
  2424. FW_UNIT_ADDRESS(fwserial_mgmt_addr_handler.offset);
  2425. err = fw_core_add_descriptor(&fwserial_unit_directory);
  2426. if (err) {
  2427. pr_err("add unit descriptor failed (%d)\n", err);
  2428. goto remove_handler;
  2429. }
  2430. err = driver_register(&fwserial_driver.driver);
  2431. if (err) {
  2432. pr_err("register fwserial driver failed (%d)\n", err);
  2433. goto remove_descriptor;
  2434. }
  2435. return 0;
  2436. remove_descriptor:
  2437. fw_core_remove_descriptor(&fwserial_unit_directory);
  2438. remove_handler:
  2439. fw_core_remove_address_handler(&fwserial_mgmt_addr_handler);
  2440. destroy_cache:
  2441. kmem_cache_destroy(fwtty_txn_cache);
  2442. unregister_loop:
  2443. if (create_loop_dev)
  2444. tty_unregister_driver(fwloop_driver);
  2445. put_loop:
  2446. if (create_loop_dev)
  2447. put_tty_driver(fwloop_driver);
  2448. unregister_driver:
  2449. tty_unregister_driver(fwtty_driver);
  2450. put_tty:
  2451. put_tty_driver(fwtty_driver);
  2452. remove_debugfs:
  2453. debugfs_remove_recursive(fwserial_debugfs);
  2454. return err;
  2455. }
  2456. static void __exit fwserial_exit(void)
  2457. {
  2458. driver_unregister(&fwserial_driver.driver);
  2459. fw_core_remove_descriptor(&fwserial_unit_directory);
  2460. fw_core_remove_address_handler(&fwserial_mgmt_addr_handler);
  2461. kmem_cache_destroy(fwtty_txn_cache);
  2462. if (create_loop_dev) {
  2463. tty_unregister_driver(fwloop_driver);
  2464. put_tty_driver(fwloop_driver);
  2465. }
  2466. tty_unregister_driver(fwtty_driver);
  2467. put_tty_driver(fwtty_driver);
  2468. debugfs_remove_recursive(fwserial_debugfs);
  2469. }
  2470. module_init(fwserial_init);
  2471. module_exit(fwserial_exit);
  2472. MODULE_AUTHOR("Peter Hurley (peter@hurleysoftware.com)");
  2473. MODULE_DESCRIPTION("FireWire Serial TTY Driver");
  2474. MODULE_LICENSE("GPL");
  2475. MODULE_DEVICE_TABLE(ieee1394, fwserial_id_table);
  2476. MODULE_PARM_DESC(ttys, "Number of ttys to create for each local firewire node");
  2477. MODULE_PARM_DESC(auto, "Auto-connect a tty to each firewire node discovered");
  2478. MODULE_PARM_DESC(loop, "Create a loopback device, fwloop<n>, with ttys");