hso.c 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401
  1. /******************************************************************************
  2. *
  3. * Driver for Option High Speed Mobile Devices.
  4. *
  5. * Copyright (C) 2008 Option International
  6. * Filip Aben <f.aben@option.com>
  7. * Denis Joseph Barrow <d.barow@option.com>
  8. * Jan Dumon <j.dumon@option.com>
  9. * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
  10. * <ajb@spheresystems.co.uk>
  11. * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
  12. * Copyright (C) 2008 Novell, Inc.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  26. * USA
  27. *
  28. *
  29. *****************************************************************************/
  30. /******************************************************************************
  31. *
  32. * Description of the device:
  33. *
  34. * Interface 0: Contains the IP network interface on the bulk end points.
  35. * The multiplexed serial ports are using the interrupt and
  36. * control endpoints.
  37. * Interrupt contains a bitmap telling which multiplexed
  38. * serialport needs servicing.
  39. *
  40. * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
  41. * port is opened, as this have a huge impact on the network port
  42. * throughput.
  43. *
  44. * Interface 2: Standard modem interface - circuit switched interface, this
  45. * can be used to make a standard ppp connection however it
  46. * should not be used in conjunction with the IP network interface
  47. * enabled for USB performance reasons i.e. if using this set
  48. * ideally disable_net=1.
  49. *
  50. *****************************************************************************/
  51. #include <linux/sched.h>
  52. #include <linux/slab.h>
  53. #include <linux/init.h>
  54. #include <linux/delay.h>
  55. #include <linux/netdevice.h>
  56. #include <linux/module.h>
  57. #include <linux/ethtool.h>
  58. #include <linux/usb.h>
  59. #include <linux/timer.h>
  60. #include <linux/tty.h>
  61. #include <linux/tty_driver.h>
  62. #include <linux/tty_flip.h>
  63. #include <linux/kmod.h>
  64. #include <linux/rfkill.h>
  65. #include <linux/ip.h>
  66. #include <linux/uaccess.h>
  67. #include <linux/usb/cdc.h>
  68. #include <net/arp.h>
  69. #include <asm/byteorder.h>
  70. #include <linux/serial_core.h>
  71. #include <linux/serial.h>
  72. #define MOD_AUTHOR "Option Wireless"
  73. #define MOD_DESCRIPTION "USB High Speed Option driver"
  74. #define MOD_LICENSE "GPL"
  75. #define HSO_MAX_NET_DEVICES 10
  76. #define HSO__MAX_MTU 2048
  77. #define DEFAULT_MTU 1500
  78. #define DEFAULT_MRU 1500
  79. #define CTRL_URB_RX_SIZE 1024
  80. #define CTRL_URB_TX_SIZE 64
  81. #define BULK_URB_RX_SIZE 4096
  82. #define BULK_URB_TX_SIZE 8192
  83. #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
  84. #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
  85. #define MUX_BULK_RX_BUF_COUNT 4
  86. #define USB_TYPE_OPTION_VENDOR 0x20
  87. /* These definitions are used with the struct hso_net flags element */
  88. /* - use *_bit operations on it. (bit indices not values.) */
  89. #define HSO_NET_RUNNING 0
  90. #define HSO_NET_TX_TIMEOUT (HZ*10)
  91. #define HSO_SERIAL_MAGIC 0x48534f31
  92. /* Number of ttys to handle */
  93. #define HSO_SERIAL_TTY_MINORS 256
  94. #define MAX_RX_URBS 2
  95. static inline struct hso_serial *get_serial_by_tty(struct tty_struct *tty)
  96. {
  97. if (tty)
  98. return tty->driver_data;
  99. return NULL;
  100. }
  101. /*****************************************************************************/
  102. /* Debugging functions */
  103. /*****************************************************************************/
  104. #define D__(lvl_, fmt, arg...) \
  105. do { \
  106. printk(lvl_ "[%d:%s]: " fmt "\n", \
  107. __LINE__, __func__, ## arg); \
  108. } while (0)
  109. #define D_(lvl, args...) \
  110. do { \
  111. if (lvl & debug) \
  112. D__(KERN_INFO, args); \
  113. } while (0)
  114. #define D1(args...) D_(0x01, ##args)
  115. #define D2(args...) D_(0x02, ##args)
  116. #define D3(args...) D_(0x04, ##args)
  117. #define D4(args...) D_(0x08, ##args)
  118. #define D5(args...) D_(0x10, ##args)
  119. /*****************************************************************************/
  120. /* Enumerators */
  121. /*****************************************************************************/
  122. enum pkt_parse_state {
  123. WAIT_IP,
  124. WAIT_DATA,
  125. WAIT_SYNC
  126. };
  127. /*****************************************************************************/
  128. /* Structs */
  129. /*****************************************************************************/
  130. struct hso_shared_int {
  131. struct usb_endpoint_descriptor *intr_endp;
  132. void *shared_intr_buf;
  133. struct urb *shared_intr_urb;
  134. struct usb_device *usb;
  135. int use_count;
  136. int ref_count;
  137. struct mutex shared_int_lock;
  138. };
  139. struct hso_net {
  140. struct hso_device *parent;
  141. struct net_device *net;
  142. struct rfkill *rfkill;
  143. struct usb_endpoint_descriptor *in_endp;
  144. struct usb_endpoint_descriptor *out_endp;
  145. struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
  146. struct urb *mux_bulk_tx_urb;
  147. void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
  148. void *mux_bulk_tx_buf;
  149. struct sk_buff *skb_rx_buf;
  150. struct sk_buff *skb_tx_buf;
  151. enum pkt_parse_state rx_parse_state;
  152. spinlock_t net_lock;
  153. unsigned short rx_buf_size;
  154. unsigned short rx_buf_missing;
  155. struct iphdr rx_ip_hdr;
  156. unsigned long flags;
  157. };
  158. enum rx_ctrl_state{
  159. RX_IDLE,
  160. RX_SENT,
  161. RX_PENDING
  162. };
  163. #define BM_REQUEST_TYPE (0xa1)
  164. #define B_NOTIFICATION (0x20)
  165. #define W_VALUE (0x0)
  166. #define W_INDEX (0x2)
  167. #define W_LENGTH (0x2)
  168. #define B_OVERRUN (0x1<<6)
  169. #define B_PARITY (0x1<<5)
  170. #define B_FRAMING (0x1<<4)
  171. #define B_RING_SIGNAL (0x1<<3)
  172. #define B_BREAK (0x1<<2)
  173. #define B_TX_CARRIER (0x1<<1)
  174. #define B_RX_CARRIER (0x1<<0)
  175. struct hso_serial_state_notification {
  176. u8 bmRequestType;
  177. u8 bNotification;
  178. u16 wValue;
  179. u16 wIndex;
  180. u16 wLength;
  181. u16 UART_state_bitmap;
  182. } __packed;
  183. struct hso_tiocmget {
  184. struct mutex mutex;
  185. wait_queue_head_t waitq;
  186. int intr_completed;
  187. struct usb_endpoint_descriptor *endp;
  188. struct urb *urb;
  189. struct hso_serial_state_notification serial_state_notification;
  190. u16 prev_UART_state_bitmap;
  191. struct uart_icount icount;
  192. };
  193. struct hso_serial {
  194. struct hso_device *parent;
  195. int magic;
  196. u8 minor;
  197. struct hso_shared_int *shared_int;
  198. /* rx/tx urb could be either a bulk urb or a control urb depending
  199. on which serial port it is used on. */
  200. struct urb *rx_urb[MAX_RX_URBS];
  201. u8 num_rx_urbs;
  202. u8 *rx_data[MAX_RX_URBS];
  203. u16 rx_data_length; /* should contain allocated length */
  204. struct urb *tx_urb;
  205. u8 *tx_data;
  206. u8 *tx_buffer;
  207. u16 tx_data_length; /* should contain allocated length */
  208. u16 tx_data_count;
  209. u16 tx_buffer_count;
  210. struct usb_ctrlrequest ctrl_req_tx;
  211. struct usb_ctrlrequest ctrl_req_rx;
  212. struct usb_endpoint_descriptor *in_endp;
  213. struct usb_endpoint_descriptor *out_endp;
  214. enum rx_ctrl_state rx_state;
  215. u8 rts_state;
  216. u8 dtr_state;
  217. unsigned tx_urb_used:1;
  218. /* from usb_serial_port */
  219. struct tty_struct *tty;
  220. int open_count;
  221. spinlock_t serial_lock;
  222. int (*write_data) (struct hso_serial *serial);
  223. struct hso_tiocmget *tiocmget;
  224. /* Hacks required to get flow control
  225. * working on the serial receive buffers
  226. * so as not to drop characters on the floor.
  227. */
  228. int curr_rx_urb_idx;
  229. u16 curr_rx_urb_offset;
  230. u8 rx_urb_filled[MAX_RX_URBS];
  231. struct tasklet_struct unthrottle_tasklet;
  232. struct work_struct retry_unthrottle_workqueue;
  233. };
  234. struct hso_device {
  235. union {
  236. struct hso_serial *dev_serial;
  237. struct hso_net *dev_net;
  238. } port_data;
  239. u32 port_spec;
  240. u8 is_active;
  241. u8 usb_gone;
  242. struct work_struct async_get_intf;
  243. struct work_struct async_put_intf;
  244. struct work_struct reset_device;
  245. struct usb_device *usb;
  246. struct usb_interface *interface;
  247. struct device *dev;
  248. struct kref ref;
  249. struct mutex mutex;
  250. };
  251. /* Type of interface */
  252. #define HSO_INTF_MASK 0xFF00
  253. #define HSO_INTF_MUX 0x0100
  254. #define HSO_INTF_BULK 0x0200
  255. /* Type of port */
  256. #define HSO_PORT_MASK 0xFF
  257. #define HSO_PORT_NO_PORT 0x0
  258. #define HSO_PORT_CONTROL 0x1
  259. #define HSO_PORT_APP 0x2
  260. #define HSO_PORT_GPS 0x3
  261. #define HSO_PORT_PCSC 0x4
  262. #define HSO_PORT_APP2 0x5
  263. #define HSO_PORT_GPS_CONTROL 0x6
  264. #define HSO_PORT_MSD 0x7
  265. #define HSO_PORT_VOICE 0x8
  266. #define HSO_PORT_DIAG2 0x9
  267. #define HSO_PORT_DIAG 0x10
  268. #define HSO_PORT_MODEM 0x11
  269. #define HSO_PORT_NETWORK 0x12
  270. /* Additional device info */
  271. #define HSO_INFO_MASK 0xFF000000
  272. #define HSO_INFO_CRC_BUG 0x01000000
  273. /*****************************************************************************/
  274. /* Prototypes */
  275. /*****************************************************************************/
  276. /* Serial driver functions */
  277. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  278. unsigned int set, unsigned int clear);
  279. static void ctrl_callback(struct urb *urb);
  280. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
  281. static void hso_kick_transmit(struct hso_serial *serial);
  282. /* Helper functions */
  283. static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
  284. struct usb_device *usb, gfp_t gfp);
  285. static void handle_usb_error(int status, const char *function,
  286. struct hso_device *hso_dev);
  287. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  288. int type, int dir);
  289. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
  290. static void hso_free_interface(struct usb_interface *intf);
  291. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
  292. static int hso_stop_serial_device(struct hso_device *hso_dev);
  293. static int hso_start_net_device(struct hso_device *hso_dev);
  294. static void hso_free_shared_int(struct hso_shared_int *shared_int);
  295. static int hso_stop_net_device(struct hso_device *hso_dev);
  296. static void hso_serial_ref_free(struct kref *ref);
  297. static void hso_std_serial_read_bulk_callback(struct urb *urb);
  298. static int hso_mux_serial_read(struct hso_serial *serial);
  299. static void async_get_intf(struct work_struct *data);
  300. static void async_put_intf(struct work_struct *data);
  301. static int hso_put_activity(struct hso_device *hso_dev);
  302. static int hso_get_activity(struct hso_device *hso_dev);
  303. static void tiocmget_intr_callback(struct urb *urb);
  304. static void reset_device(struct work_struct *data);
  305. /*****************************************************************************/
  306. /* Helping functions */
  307. /*****************************************************************************/
  308. /* #define DEBUG */
  309. static inline struct hso_net *dev2net(struct hso_device *hso_dev)
  310. {
  311. return hso_dev->port_data.dev_net;
  312. }
  313. static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
  314. {
  315. return hso_dev->port_data.dev_serial;
  316. }
  317. /* Debugging functions */
  318. #ifdef DEBUG
  319. static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
  320. unsigned int len)
  321. {
  322. static char name[255];
  323. sprintf(name, "hso[%d:%s]", line_count, func_name);
  324. print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
  325. }
  326. #define DUMP(buf_, len_) \
  327. dbg_dump(__LINE__, __func__, (unsigned char *)buf_, len_)
  328. #define DUMP1(buf_, len_) \
  329. do { \
  330. if (0x01 & debug) \
  331. DUMP(buf_, len_); \
  332. } while (0)
  333. #else
  334. #define DUMP(buf_, len_)
  335. #define DUMP1(buf_, len_)
  336. #endif
  337. /* module parameters */
  338. static int debug;
  339. static int tty_major;
  340. static int disable_net;
  341. /* driver info */
  342. static const char driver_name[] = "hso";
  343. static const char tty_filename[] = "ttyHS";
  344. static const char *version = __FILE__ ": " MOD_AUTHOR;
  345. /* the usb driver itself (registered in hso_init) */
  346. static struct usb_driver hso_driver;
  347. /* serial structures */
  348. static struct tty_driver *tty_drv;
  349. static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
  350. static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
  351. static spinlock_t serial_table_lock;
  352. static const s32 default_port_spec[] = {
  353. HSO_INTF_MUX | HSO_PORT_NETWORK,
  354. HSO_INTF_BULK | HSO_PORT_DIAG,
  355. HSO_INTF_BULK | HSO_PORT_MODEM,
  356. 0
  357. };
  358. static const s32 icon321_port_spec[] = {
  359. HSO_INTF_MUX | HSO_PORT_NETWORK,
  360. HSO_INTF_BULK | HSO_PORT_DIAG2,
  361. HSO_INTF_BULK | HSO_PORT_MODEM,
  362. HSO_INTF_BULK | HSO_PORT_DIAG,
  363. 0
  364. };
  365. #define default_port_device(vendor, product) \
  366. USB_DEVICE(vendor, product), \
  367. .driver_info = (kernel_ulong_t)default_port_spec
  368. #define icon321_port_device(vendor, product) \
  369. USB_DEVICE(vendor, product), \
  370. .driver_info = (kernel_ulong_t)icon321_port_spec
  371. /* list of devices we support */
  372. static const struct usb_device_id hso_ids[] = {
  373. {default_port_device(0x0af0, 0x6711)},
  374. {default_port_device(0x0af0, 0x6731)},
  375. {default_port_device(0x0af0, 0x6751)},
  376. {default_port_device(0x0af0, 0x6771)},
  377. {default_port_device(0x0af0, 0x6791)},
  378. {default_port_device(0x0af0, 0x6811)},
  379. {default_port_device(0x0af0, 0x6911)},
  380. {default_port_device(0x0af0, 0x6951)},
  381. {default_port_device(0x0af0, 0x6971)},
  382. {default_port_device(0x0af0, 0x7011)},
  383. {default_port_device(0x0af0, 0x7031)},
  384. {default_port_device(0x0af0, 0x7051)},
  385. {default_port_device(0x0af0, 0x7071)},
  386. {default_port_device(0x0af0, 0x7111)},
  387. {default_port_device(0x0af0, 0x7211)},
  388. {default_port_device(0x0af0, 0x7251)},
  389. {default_port_device(0x0af0, 0x7271)},
  390. {default_port_device(0x0af0, 0x7311)},
  391. {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
  392. {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
  393. {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
  394. {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
  395. {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
  396. {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
  397. {USB_DEVICE(0x0af0, 0x7381)}, /* GE40x */
  398. {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
  399. {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
  400. {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
  401. {USB_DEVICE(0x0af0, 0x7701)},
  402. {USB_DEVICE(0x0af0, 0x7706)},
  403. {USB_DEVICE(0x0af0, 0x7801)},
  404. {USB_DEVICE(0x0af0, 0x7901)},
  405. {USB_DEVICE(0x0af0, 0x7A01)},
  406. {USB_DEVICE(0x0af0, 0x7A05)},
  407. {USB_DEVICE(0x0af0, 0x8200)},
  408. {USB_DEVICE(0x0af0, 0x8201)},
  409. {USB_DEVICE(0x0af0, 0x8300)},
  410. {USB_DEVICE(0x0af0, 0x8302)},
  411. {USB_DEVICE(0x0af0, 0x8304)},
  412. {USB_DEVICE(0x0af0, 0x8400)},
  413. {USB_DEVICE(0x0af0, 0x8600)},
  414. {USB_DEVICE(0x0af0, 0x8800)},
  415. {USB_DEVICE(0x0af0, 0x8900)},
  416. {USB_DEVICE(0x0af0, 0x9000)},
  417. {USB_DEVICE(0x0af0, 0xd035)},
  418. {USB_DEVICE(0x0af0, 0xd055)},
  419. {USB_DEVICE(0x0af0, 0xd155)},
  420. {USB_DEVICE(0x0af0, 0xd255)},
  421. {USB_DEVICE(0x0af0, 0xd057)},
  422. {USB_DEVICE(0x0af0, 0xd157)},
  423. {USB_DEVICE(0x0af0, 0xd257)},
  424. {USB_DEVICE(0x0af0, 0xd357)},
  425. {USB_DEVICE(0x0af0, 0xd058)},
  426. {USB_DEVICE(0x0af0, 0xc100)},
  427. {}
  428. };
  429. MODULE_DEVICE_TABLE(usb, hso_ids);
  430. /* Sysfs attribute */
  431. static ssize_t hso_sysfs_show_porttype(struct device *dev,
  432. struct device_attribute *attr,
  433. char *buf)
  434. {
  435. struct hso_device *hso_dev = dev_get_drvdata(dev);
  436. char *port_name;
  437. if (!hso_dev)
  438. return 0;
  439. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  440. case HSO_PORT_CONTROL:
  441. port_name = "Control";
  442. break;
  443. case HSO_PORT_APP:
  444. port_name = "Application";
  445. break;
  446. case HSO_PORT_APP2:
  447. port_name = "Application2";
  448. break;
  449. case HSO_PORT_GPS:
  450. port_name = "GPS";
  451. break;
  452. case HSO_PORT_GPS_CONTROL:
  453. port_name = "GPS Control";
  454. break;
  455. case HSO_PORT_PCSC:
  456. port_name = "PCSC";
  457. break;
  458. case HSO_PORT_DIAG:
  459. port_name = "Diagnostic";
  460. break;
  461. case HSO_PORT_DIAG2:
  462. port_name = "Diagnostic2";
  463. break;
  464. case HSO_PORT_MODEM:
  465. port_name = "Modem";
  466. break;
  467. case HSO_PORT_NETWORK:
  468. port_name = "Network";
  469. break;
  470. default:
  471. port_name = "Unknown";
  472. break;
  473. }
  474. return sprintf(buf, "%s\n", port_name);
  475. }
  476. static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
  477. static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
  478. {
  479. int idx;
  480. for (idx = 0; idx < serial->num_rx_urbs; idx++)
  481. if (serial->rx_urb[idx] == urb)
  482. return idx;
  483. dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
  484. return -1;
  485. }
  486. /* converts mux value to a port spec value */
  487. static u32 hso_mux_to_port(int mux)
  488. {
  489. u32 result;
  490. switch (mux) {
  491. case 0x1:
  492. result = HSO_PORT_CONTROL;
  493. break;
  494. case 0x2:
  495. result = HSO_PORT_APP;
  496. break;
  497. case 0x4:
  498. result = HSO_PORT_PCSC;
  499. break;
  500. case 0x8:
  501. result = HSO_PORT_GPS;
  502. break;
  503. case 0x10:
  504. result = HSO_PORT_APP2;
  505. break;
  506. default:
  507. result = HSO_PORT_NO_PORT;
  508. }
  509. return result;
  510. }
  511. /* converts port spec value to a mux value */
  512. static u32 hso_port_to_mux(int port)
  513. {
  514. u32 result;
  515. switch (port & HSO_PORT_MASK) {
  516. case HSO_PORT_CONTROL:
  517. result = 0x0;
  518. break;
  519. case HSO_PORT_APP:
  520. result = 0x1;
  521. break;
  522. case HSO_PORT_PCSC:
  523. result = 0x2;
  524. break;
  525. case HSO_PORT_GPS:
  526. result = 0x3;
  527. break;
  528. case HSO_PORT_APP2:
  529. result = 0x4;
  530. break;
  531. default:
  532. result = 0x0;
  533. }
  534. return result;
  535. }
  536. static struct hso_serial *get_serial_by_shared_int_and_type(
  537. struct hso_shared_int *shared_int,
  538. int mux)
  539. {
  540. int i, port;
  541. port = hso_mux_to_port(mux);
  542. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  543. if (serial_table[i] &&
  544. (dev2ser(serial_table[i])->shared_int == shared_int) &&
  545. ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
  546. return dev2ser(serial_table[i]);
  547. }
  548. }
  549. return NULL;
  550. }
  551. static struct hso_serial *get_serial_by_index(unsigned index)
  552. {
  553. struct hso_serial *serial = NULL;
  554. unsigned long flags;
  555. spin_lock_irqsave(&serial_table_lock, flags);
  556. if (serial_table[index])
  557. serial = dev2ser(serial_table[index]);
  558. spin_unlock_irqrestore(&serial_table_lock, flags);
  559. return serial;
  560. }
  561. static int get_free_serial_index(void)
  562. {
  563. int index;
  564. unsigned long flags;
  565. spin_lock_irqsave(&serial_table_lock, flags);
  566. for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
  567. if (serial_table[index] == NULL) {
  568. spin_unlock_irqrestore(&serial_table_lock, flags);
  569. return index;
  570. }
  571. }
  572. spin_unlock_irqrestore(&serial_table_lock, flags);
  573. printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
  574. return -1;
  575. }
  576. static void set_serial_by_index(unsigned index, struct hso_serial *serial)
  577. {
  578. unsigned long flags;
  579. spin_lock_irqsave(&serial_table_lock, flags);
  580. if (serial)
  581. serial_table[index] = serial->parent;
  582. else
  583. serial_table[index] = NULL;
  584. spin_unlock_irqrestore(&serial_table_lock, flags);
  585. }
  586. static void handle_usb_error(int status, const char *function,
  587. struct hso_device *hso_dev)
  588. {
  589. char *explanation;
  590. switch (status) {
  591. case -ENODEV:
  592. explanation = "no device";
  593. break;
  594. case -ENOENT:
  595. explanation = "endpoint not enabled";
  596. break;
  597. case -EPIPE:
  598. explanation = "endpoint stalled";
  599. break;
  600. case -ENOSPC:
  601. explanation = "not enough bandwidth";
  602. break;
  603. case -ESHUTDOWN:
  604. explanation = "device disabled";
  605. break;
  606. case -EHOSTUNREACH:
  607. explanation = "device suspended";
  608. break;
  609. case -EINVAL:
  610. case -EAGAIN:
  611. case -EFBIG:
  612. case -EMSGSIZE:
  613. explanation = "internal error";
  614. break;
  615. case -EILSEQ:
  616. case -EPROTO:
  617. case -ETIME:
  618. case -ETIMEDOUT:
  619. explanation = "protocol error";
  620. if (hso_dev)
  621. schedule_work(&hso_dev->reset_device);
  622. break;
  623. default:
  624. explanation = "unknown status";
  625. break;
  626. }
  627. /* log a meaningful explanation of an USB status */
  628. D1("%s: received USB status - %s (%d)", function, explanation, status);
  629. }
  630. /* Network interface functions */
  631. /* called when net interface is brought up by ifconfig */
  632. static int hso_net_open(struct net_device *net)
  633. {
  634. struct hso_net *odev = netdev_priv(net);
  635. unsigned long flags = 0;
  636. if (!odev) {
  637. dev_err(&net->dev, "No net device !\n");
  638. return -ENODEV;
  639. }
  640. odev->skb_tx_buf = NULL;
  641. /* setup environment */
  642. spin_lock_irqsave(&odev->net_lock, flags);
  643. odev->rx_parse_state = WAIT_IP;
  644. odev->rx_buf_size = 0;
  645. odev->rx_buf_missing = sizeof(struct iphdr);
  646. spin_unlock_irqrestore(&odev->net_lock, flags);
  647. /* We are up and running. */
  648. set_bit(HSO_NET_RUNNING, &odev->flags);
  649. hso_start_net_device(odev->parent);
  650. /* Tell the kernel we are ready to start receiving from it */
  651. netif_start_queue(net);
  652. return 0;
  653. }
  654. /* called when interface is brought down by ifconfig */
  655. static int hso_net_close(struct net_device *net)
  656. {
  657. struct hso_net *odev = netdev_priv(net);
  658. /* we don't need the queue anymore */
  659. netif_stop_queue(net);
  660. /* no longer running */
  661. clear_bit(HSO_NET_RUNNING, &odev->flags);
  662. hso_stop_net_device(odev->parent);
  663. /* done */
  664. return 0;
  665. }
  666. /* USB tells is xmit done, we should start the netqueue again */
  667. static void write_bulk_callback(struct urb *urb)
  668. {
  669. struct hso_net *odev = urb->context;
  670. int status = urb->status;
  671. /* Sanity check */
  672. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  673. dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
  674. return;
  675. }
  676. /* Do we still have a valid kernel network device? */
  677. if (!netif_device_present(odev->net)) {
  678. dev_err(&urb->dev->dev, "%s: net device not present\n",
  679. __func__);
  680. return;
  681. }
  682. /* log status, but don't act on it, we don't need to resubmit anything
  683. * anyhow */
  684. if (status)
  685. handle_usb_error(status, __func__, odev->parent);
  686. hso_put_activity(odev->parent);
  687. /* Tell the network interface we are ready for another frame */
  688. netif_wake_queue(odev->net);
  689. }
  690. /* called by kernel when we need to transmit a packet */
  691. static netdev_tx_t hso_net_start_xmit(struct sk_buff *skb,
  692. struct net_device *net)
  693. {
  694. struct hso_net *odev = netdev_priv(net);
  695. int result;
  696. /* Tell the kernel, "No more frames 'til we are done with this one." */
  697. netif_stop_queue(net);
  698. if (hso_get_activity(odev->parent) == -EAGAIN) {
  699. odev->skb_tx_buf = skb;
  700. return NETDEV_TX_OK;
  701. }
  702. /* log if asked */
  703. DUMP1(skb->data, skb->len);
  704. /* Copy it from kernel memory to OUR memory */
  705. memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
  706. D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
  707. /* Fill in the URB for shipping it out. */
  708. usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
  709. odev->parent->usb,
  710. usb_sndbulkpipe(odev->parent->usb,
  711. odev->out_endp->
  712. bEndpointAddress & 0x7F),
  713. odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
  714. odev);
  715. /* Deal with the Zero Length packet problem, I hope */
  716. odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
  717. /* Send the URB on its merry way. */
  718. result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
  719. if (result) {
  720. dev_warn(&odev->parent->interface->dev,
  721. "failed mux_bulk_tx_urb %d\n", result);
  722. net->stats.tx_errors++;
  723. netif_start_queue(net);
  724. } else {
  725. net->stats.tx_packets++;
  726. net->stats.tx_bytes += skb->len;
  727. }
  728. dev_kfree_skb(skb);
  729. /* we're done */
  730. return NETDEV_TX_OK;
  731. }
  732. static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
  733. {
  734. struct hso_net *odev = netdev_priv(net);
  735. strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
  736. usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info);
  737. }
  738. static const struct ethtool_ops ops = {
  739. .get_drvinfo = hso_get_drvinfo,
  740. .get_link = ethtool_op_get_link
  741. };
  742. /* called when a packet did not ack after watchdogtimeout */
  743. static void hso_net_tx_timeout(struct net_device *net)
  744. {
  745. struct hso_net *odev = netdev_priv(net);
  746. if (!odev)
  747. return;
  748. /* Tell syslog we are hosed. */
  749. dev_warn(&net->dev, "Tx timed out.\n");
  750. /* Tear the waiting frame off the list */
  751. if (odev->mux_bulk_tx_urb &&
  752. (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
  753. usb_unlink_urb(odev->mux_bulk_tx_urb);
  754. /* Update statistics */
  755. net->stats.tx_errors++;
  756. }
  757. /* make a real packet from the received USB buffer */
  758. static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
  759. unsigned int count, unsigned char is_eop)
  760. {
  761. unsigned short temp_bytes;
  762. unsigned short buffer_offset = 0;
  763. unsigned short frame_len;
  764. unsigned char *tmp_rx_buf;
  765. /* log if needed */
  766. D1("Rx %d bytes", count);
  767. DUMP(ip_pkt, min(128, (int)count));
  768. while (count) {
  769. switch (odev->rx_parse_state) {
  770. case WAIT_IP:
  771. /* waiting for IP header. */
  772. /* wanted bytes - size of ip header */
  773. temp_bytes =
  774. (count <
  775. odev->rx_buf_missing) ? count : odev->
  776. rx_buf_missing;
  777. memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
  778. odev->rx_buf_size, ip_pkt + buffer_offset,
  779. temp_bytes);
  780. odev->rx_buf_size += temp_bytes;
  781. buffer_offset += temp_bytes;
  782. odev->rx_buf_missing -= temp_bytes;
  783. count -= temp_bytes;
  784. if (!odev->rx_buf_missing) {
  785. /* header is complete allocate an sk_buffer and
  786. * continue to WAIT_DATA */
  787. frame_len = ntohs(odev->rx_ip_hdr.tot_len);
  788. if ((frame_len > DEFAULT_MRU) ||
  789. (frame_len < sizeof(struct iphdr))) {
  790. dev_err(&odev->net->dev,
  791. "Invalid frame (%d) length\n",
  792. frame_len);
  793. odev->rx_parse_state = WAIT_SYNC;
  794. continue;
  795. }
  796. /* Allocate an sk_buff */
  797. odev->skb_rx_buf = netdev_alloc_skb(odev->net,
  798. frame_len);
  799. if (!odev->skb_rx_buf) {
  800. /* We got no receive buffer. */
  801. D1("could not allocate memory");
  802. odev->rx_parse_state = WAIT_SYNC;
  803. return;
  804. }
  805. /* Copy what we got so far. make room for iphdr
  806. * after tail. */
  807. tmp_rx_buf =
  808. skb_put(odev->skb_rx_buf,
  809. sizeof(struct iphdr));
  810. memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
  811. sizeof(struct iphdr));
  812. /* ETH_HLEN */
  813. odev->rx_buf_size = sizeof(struct iphdr);
  814. /* Filip actually use .tot_len */
  815. odev->rx_buf_missing =
  816. frame_len - sizeof(struct iphdr);
  817. odev->rx_parse_state = WAIT_DATA;
  818. }
  819. break;
  820. case WAIT_DATA:
  821. temp_bytes = (count < odev->rx_buf_missing)
  822. ? count : odev->rx_buf_missing;
  823. /* Copy the rest of the bytes that are left in the
  824. * buffer into the waiting sk_buf. */
  825. /* Make room for temp_bytes after tail. */
  826. tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
  827. memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
  828. odev->rx_buf_missing -= temp_bytes;
  829. count -= temp_bytes;
  830. buffer_offset += temp_bytes;
  831. odev->rx_buf_size += temp_bytes;
  832. if (!odev->rx_buf_missing) {
  833. /* Packet is complete. Inject into stack. */
  834. /* We have IP packet here */
  835. odev->skb_rx_buf->protocol = cpu_to_be16(ETH_P_IP);
  836. /* don't check it */
  837. odev->skb_rx_buf->ip_summed =
  838. CHECKSUM_UNNECESSARY;
  839. skb_reset_mac_header(odev->skb_rx_buf);
  840. /* Ship it off to the kernel */
  841. netif_rx(odev->skb_rx_buf);
  842. /* No longer our buffer. */
  843. odev->skb_rx_buf = NULL;
  844. /* update out statistics */
  845. odev->net->stats.rx_packets++;
  846. odev->net->stats.rx_bytes += odev->rx_buf_size;
  847. odev->rx_buf_size = 0;
  848. odev->rx_buf_missing = sizeof(struct iphdr);
  849. odev->rx_parse_state = WAIT_IP;
  850. }
  851. break;
  852. case WAIT_SYNC:
  853. D1(" W_S");
  854. count = 0;
  855. break;
  856. default:
  857. D1(" ");
  858. count--;
  859. break;
  860. }
  861. }
  862. /* Recovery mechanism for WAIT_SYNC state. */
  863. if (is_eop) {
  864. if (odev->rx_parse_state == WAIT_SYNC) {
  865. odev->rx_parse_state = WAIT_IP;
  866. odev->rx_buf_size = 0;
  867. odev->rx_buf_missing = sizeof(struct iphdr);
  868. }
  869. }
  870. }
  871. /* Moving data from usb to kernel (in interrupt state) */
  872. static void read_bulk_callback(struct urb *urb)
  873. {
  874. struct hso_net *odev = urb->context;
  875. struct net_device *net;
  876. int result;
  877. int status = urb->status;
  878. /* is al ok? (Filip: Who's Al ?) */
  879. if (status) {
  880. handle_usb_error(status, __func__, odev->parent);
  881. return;
  882. }
  883. /* Sanity check */
  884. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  885. D1("BULK IN callback but driver is not active!");
  886. return;
  887. }
  888. usb_mark_last_busy(urb->dev);
  889. net = odev->net;
  890. if (!netif_device_present(net)) {
  891. /* Somebody killed our network interface... */
  892. return;
  893. }
  894. if (odev->parent->port_spec & HSO_INFO_CRC_BUG) {
  895. u32 rest;
  896. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  897. rest = urb->actual_length %
  898. le16_to_cpu(odev->in_endp->wMaxPacketSize);
  899. if (((rest == 5) || (rest == 6)) &&
  900. !memcmp(((u8 *) urb->transfer_buffer) +
  901. urb->actual_length - 4, crc_check, 4)) {
  902. urb->actual_length -= 4;
  903. }
  904. }
  905. /* do we even have a packet? */
  906. if (urb->actual_length) {
  907. /* Handle the IP stream, add header and push it onto network
  908. * stack if the packet is complete. */
  909. spin_lock(&odev->net_lock);
  910. packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
  911. (urb->transfer_buffer_length >
  912. urb->actual_length) ? 1 : 0);
  913. spin_unlock(&odev->net_lock);
  914. }
  915. /* We are done with this URB, resubmit it. Prep the USB to wait for
  916. * another frame. Reuse same as received. */
  917. usb_fill_bulk_urb(urb,
  918. odev->parent->usb,
  919. usb_rcvbulkpipe(odev->parent->usb,
  920. odev->in_endp->
  921. bEndpointAddress & 0x7F),
  922. urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
  923. read_bulk_callback, odev);
  924. /* Give this to the USB subsystem so it can tell us when more data
  925. * arrives. */
  926. result = usb_submit_urb(urb, GFP_ATOMIC);
  927. if (result)
  928. dev_warn(&odev->parent->interface->dev,
  929. "%s failed submit mux_bulk_rx_urb %d\n", __func__,
  930. result);
  931. }
  932. /* Serial driver functions */
  933. static void hso_init_termios(struct ktermios *termios)
  934. {
  935. /*
  936. * The default requirements for this device are:
  937. */
  938. termios->c_iflag &=
  939. ~(IGNBRK /* disable ignore break */
  940. | BRKINT /* disable break causes interrupt */
  941. | PARMRK /* disable mark parity errors */
  942. | ISTRIP /* disable clear high bit of input characters */
  943. | INLCR /* disable translate NL to CR */
  944. | IGNCR /* disable ignore CR */
  945. | ICRNL /* disable translate CR to NL */
  946. | IXON); /* disable enable XON/XOFF flow control */
  947. /* disable postprocess output characters */
  948. termios->c_oflag &= ~OPOST;
  949. termios->c_lflag &=
  950. ~(ECHO /* disable echo input characters */
  951. | ECHONL /* disable echo new line */
  952. | ICANON /* disable erase, kill, werase, and rprnt
  953. special characters */
  954. | ISIG /* disable interrupt, quit, and suspend special
  955. characters */
  956. | IEXTEN); /* disable non-POSIX special characters */
  957. termios->c_cflag &=
  958. ~(CSIZE /* no size */
  959. | PARENB /* disable parity bit */
  960. | CBAUD /* clear current baud rate */
  961. | CBAUDEX); /* clear current buad rate */
  962. termios->c_cflag |= CS8; /* character size 8 bits */
  963. /* baud rate 115200 */
  964. tty_termios_encode_baud_rate(termios, 115200, 115200);
  965. }
  966. static void _hso_serial_set_termios(struct tty_struct *tty,
  967. struct ktermios *old)
  968. {
  969. struct hso_serial *serial = get_serial_by_tty(tty);
  970. struct ktermios *termios;
  971. if (!serial) {
  972. printk(KERN_ERR "%s: no tty structures", __func__);
  973. return;
  974. }
  975. D4("port %d", serial->minor);
  976. /*
  977. * Fix up unsupported bits
  978. */
  979. termios = tty->termios;
  980. termios->c_iflag &= ~IXON; /* disable enable XON/XOFF flow control */
  981. termios->c_cflag &=
  982. ~(CSIZE /* no size */
  983. | PARENB /* disable parity bit */
  984. | CBAUD /* clear current baud rate */
  985. | CBAUDEX); /* clear current buad rate */
  986. termios->c_cflag |= CS8; /* character size 8 bits */
  987. /* baud rate 115200 */
  988. tty_encode_baud_rate(tty, 115200, 115200);
  989. }
  990. static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
  991. {
  992. int result;
  993. /* We are done with this URB, resubmit it. Prep the USB to wait for
  994. * another frame */
  995. usb_fill_bulk_urb(urb, serial->parent->usb,
  996. usb_rcvbulkpipe(serial->parent->usb,
  997. serial->in_endp->
  998. bEndpointAddress & 0x7F),
  999. urb->transfer_buffer, serial->rx_data_length,
  1000. hso_std_serial_read_bulk_callback, serial);
  1001. /* Give this to the USB subsystem so it can tell us when more data
  1002. * arrives. */
  1003. result = usb_submit_urb(urb, GFP_ATOMIC);
  1004. if (result) {
  1005. dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
  1006. __func__, result);
  1007. }
  1008. }
  1009. static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
  1010. {
  1011. int count;
  1012. struct urb *curr_urb;
  1013. while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
  1014. curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
  1015. count = put_rxbuf_data(curr_urb, serial);
  1016. if (count == -1)
  1017. return;
  1018. if (count == 0) {
  1019. serial->curr_rx_urb_idx++;
  1020. if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
  1021. serial->curr_rx_urb_idx = 0;
  1022. hso_resubmit_rx_bulk_urb(serial, curr_urb);
  1023. }
  1024. }
  1025. }
  1026. static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
  1027. {
  1028. int count = 0;
  1029. struct urb *urb;
  1030. urb = serial->rx_urb[0];
  1031. if (serial->open_count > 0) {
  1032. count = put_rxbuf_data(urb, serial);
  1033. if (count == -1)
  1034. return;
  1035. }
  1036. /* Re issue a read as long as we receive data. */
  1037. if (count == 0 && ((urb->actual_length != 0) ||
  1038. (serial->rx_state == RX_PENDING))) {
  1039. serial->rx_state = RX_SENT;
  1040. hso_mux_serial_read(serial);
  1041. } else
  1042. serial->rx_state = RX_IDLE;
  1043. }
  1044. /* read callback for Diag and CS port */
  1045. static void hso_std_serial_read_bulk_callback(struct urb *urb)
  1046. {
  1047. struct hso_serial *serial = urb->context;
  1048. int status = urb->status;
  1049. /* sanity check */
  1050. if (!serial) {
  1051. D1("serial == NULL");
  1052. return;
  1053. } else if (status) {
  1054. handle_usb_error(status, __func__, serial->parent);
  1055. return;
  1056. }
  1057. D4("\n--- Got serial_read_bulk callback %02x ---", status);
  1058. D1("Actual length = %d\n", urb->actual_length);
  1059. DUMP1(urb->transfer_buffer, urb->actual_length);
  1060. /* Anyone listening? */
  1061. if (serial->open_count == 0)
  1062. return;
  1063. if (status == 0) {
  1064. if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
  1065. u32 rest;
  1066. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  1067. rest =
  1068. urb->actual_length %
  1069. le16_to_cpu(serial->in_endp->wMaxPacketSize);
  1070. if (((rest == 5) || (rest == 6)) &&
  1071. !memcmp(((u8 *) urb->transfer_buffer) +
  1072. urb->actual_length - 4, crc_check, 4)) {
  1073. urb->actual_length -= 4;
  1074. }
  1075. }
  1076. /* Valid data, handle RX data */
  1077. spin_lock(&serial->serial_lock);
  1078. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
  1079. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1080. spin_unlock(&serial->serial_lock);
  1081. } else if (status == -ENOENT || status == -ECONNRESET) {
  1082. /* Unlinked - check for throttled port. */
  1083. D2("Port %d, successfully unlinked urb", serial->minor);
  1084. spin_lock(&serial->serial_lock);
  1085. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1086. hso_resubmit_rx_bulk_urb(serial, urb);
  1087. spin_unlock(&serial->serial_lock);
  1088. } else {
  1089. D2("Port %d, status = %d for read urb", serial->minor, status);
  1090. return;
  1091. }
  1092. }
  1093. /*
  1094. * This needs to be a tasklet otherwise we will
  1095. * end up recursively calling this function.
  1096. */
  1097. static void hso_unthrottle_tasklet(struct hso_serial *serial)
  1098. {
  1099. unsigned long flags;
  1100. spin_lock_irqsave(&serial->serial_lock, flags);
  1101. if ((serial->parent->port_spec & HSO_INTF_MUX))
  1102. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1103. else
  1104. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1105. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1106. }
  1107. static void hso_unthrottle(struct tty_struct *tty)
  1108. {
  1109. struct hso_serial *serial = get_serial_by_tty(tty);
  1110. tasklet_hi_schedule(&serial->unthrottle_tasklet);
  1111. }
  1112. static void hso_unthrottle_workfunc(struct work_struct *work)
  1113. {
  1114. struct hso_serial *serial =
  1115. container_of(work, struct hso_serial,
  1116. retry_unthrottle_workqueue);
  1117. hso_unthrottle_tasklet(serial);
  1118. }
  1119. /* open the requested serial port */
  1120. static int hso_serial_open(struct tty_struct *tty, struct file *filp)
  1121. {
  1122. struct hso_serial *serial = get_serial_by_index(tty->index);
  1123. int result;
  1124. /* sanity check */
  1125. if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
  1126. WARN_ON(1);
  1127. tty->driver_data = NULL;
  1128. D1("Failed to open port");
  1129. return -ENODEV;
  1130. }
  1131. mutex_lock(&serial->parent->mutex);
  1132. result = usb_autopm_get_interface(serial->parent->interface);
  1133. if (result < 0)
  1134. goto err_out;
  1135. D1("Opening %d", serial->minor);
  1136. kref_get(&serial->parent->ref);
  1137. /* setup */
  1138. spin_lock_irq(&serial->serial_lock);
  1139. tty->driver_data = serial;
  1140. tty_kref_put(serial->tty);
  1141. serial->tty = tty_kref_get(tty);
  1142. spin_unlock_irq(&serial->serial_lock);
  1143. /* check for port already opened, if not set the termios */
  1144. serial->open_count++;
  1145. if (serial->open_count == 1) {
  1146. serial->rx_state = RX_IDLE;
  1147. /* Force default termio settings */
  1148. _hso_serial_set_termios(tty, NULL);
  1149. tasklet_init(&serial->unthrottle_tasklet,
  1150. (void (*)(unsigned long))hso_unthrottle_tasklet,
  1151. (unsigned long)serial);
  1152. INIT_WORK(&serial->retry_unthrottle_workqueue,
  1153. hso_unthrottle_workfunc);
  1154. result = hso_start_serial_device(serial->parent, GFP_KERNEL);
  1155. if (result) {
  1156. hso_stop_serial_device(serial->parent);
  1157. serial->open_count--;
  1158. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1159. }
  1160. } else {
  1161. D1("Port was already open");
  1162. }
  1163. usb_autopm_put_interface(serial->parent->interface);
  1164. /* done */
  1165. if (result)
  1166. hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
  1167. err_out:
  1168. mutex_unlock(&serial->parent->mutex);
  1169. return result;
  1170. }
  1171. /* close the requested serial port */
  1172. static void hso_serial_close(struct tty_struct *tty, struct file *filp)
  1173. {
  1174. struct hso_serial *serial = tty->driver_data;
  1175. u8 usb_gone;
  1176. D1("Closing serial port");
  1177. /* Open failed, no close cleanup required */
  1178. if (serial == NULL)
  1179. return;
  1180. mutex_lock(&serial->parent->mutex);
  1181. usb_gone = serial->parent->usb_gone;
  1182. if (!usb_gone)
  1183. usb_autopm_get_interface(serial->parent->interface);
  1184. /* reset the rts and dtr */
  1185. /* do the actual close */
  1186. serial->open_count--;
  1187. if (serial->open_count <= 0) {
  1188. serial->open_count = 0;
  1189. spin_lock_irq(&serial->serial_lock);
  1190. if (serial->tty == tty) {
  1191. serial->tty->driver_data = NULL;
  1192. serial->tty = NULL;
  1193. tty_kref_put(tty);
  1194. }
  1195. spin_unlock_irq(&serial->serial_lock);
  1196. if (!usb_gone)
  1197. hso_stop_serial_device(serial->parent);
  1198. tasklet_kill(&serial->unthrottle_tasklet);
  1199. cancel_work_sync(&serial->retry_unthrottle_workqueue);
  1200. }
  1201. if (!usb_gone)
  1202. usb_autopm_put_interface(serial->parent->interface);
  1203. mutex_unlock(&serial->parent->mutex);
  1204. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1205. }
  1206. /* close the requested serial port */
  1207. static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
  1208. int count)
  1209. {
  1210. struct hso_serial *serial = get_serial_by_tty(tty);
  1211. int space, tx_bytes;
  1212. unsigned long flags;
  1213. /* sanity check */
  1214. if (serial == NULL) {
  1215. printk(KERN_ERR "%s: serial is NULL\n", __func__);
  1216. return -ENODEV;
  1217. }
  1218. spin_lock_irqsave(&serial->serial_lock, flags);
  1219. space = serial->tx_data_length - serial->tx_buffer_count;
  1220. tx_bytes = (count < space) ? count : space;
  1221. if (!tx_bytes)
  1222. goto out;
  1223. memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
  1224. serial->tx_buffer_count += tx_bytes;
  1225. out:
  1226. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1227. hso_kick_transmit(serial);
  1228. /* done */
  1229. return tx_bytes;
  1230. }
  1231. /* how much room is there for writing */
  1232. static int hso_serial_write_room(struct tty_struct *tty)
  1233. {
  1234. struct hso_serial *serial = get_serial_by_tty(tty);
  1235. int room;
  1236. unsigned long flags;
  1237. spin_lock_irqsave(&serial->serial_lock, flags);
  1238. room = serial->tx_data_length - serial->tx_buffer_count;
  1239. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1240. /* return free room */
  1241. return room;
  1242. }
  1243. /* setup the term */
  1244. static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
  1245. {
  1246. struct hso_serial *serial = get_serial_by_tty(tty);
  1247. unsigned long flags;
  1248. if (old)
  1249. D5("Termios called with: cflags new[%d] - old[%d]",
  1250. tty->termios->c_cflag, old->c_cflag);
  1251. /* the actual setup */
  1252. spin_lock_irqsave(&serial->serial_lock, flags);
  1253. if (serial->open_count)
  1254. _hso_serial_set_termios(tty, old);
  1255. else
  1256. tty->termios = old;
  1257. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1258. /* done */
  1259. }
  1260. /* how many characters in the buffer */
  1261. static int hso_serial_chars_in_buffer(struct tty_struct *tty)
  1262. {
  1263. struct hso_serial *serial = get_serial_by_tty(tty);
  1264. int chars;
  1265. unsigned long flags;
  1266. /* sanity check */
  1267. if (serial == NULL)
  1268. return 0;
  1269. spin_lock_irqsave(&serial->serial_lock, flags);
  1270. chars = serial->tx_buffer_count;
  1271. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1272. return chars;
  1273. }
  1274. static int tiocmget_submit_urb(struct hso_serial *serial,
  1275. struct hso_tiocmget *tiocmget,
  1276. struct usb_device *usb)
  1277. {
  1278. int result;
  1279. if (serial->parent->usb_gone)
  1280. return -ENODEV;
  1281. usb_fill_int_urb(tiocmget->urb, usb,
  1282. usb_rcvintpipe(usb,
  1283. tiocmget->endp->
  1284. bEndpointAddress & 0x7F),
  1285. &tiocmget->serial_state_notification,
  1286. sizeof(struct hso_serial_state_notification),
  1287. tiocmget_intr_callback, serial,
  1288. tiocmget->endp->bInterval);
  1289. result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC);
  1290. if (result) {
  1291. dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__,
  1292. result);
  1293. }
  1294. return result;
  1295. }
  1296. static void tiocmget_intr_callback(struct urb *urb)
  1297. {
  1298. struct hso_serial *serial = urb->context;
  1299. struct hso_tiocmget *tiocmget;
  1300. int status = urb->status;
  1301. u16 UART_state_bitmap, prev_UART_state_bitmap;
  1302. struct uart_icount *icount;
  1303. struct hso_serial_state_notification *serial_state_notification;
  1304. struct usb_device *usb;
  1305. /* Sanity checks */
  1306. if (!serial)
  1307. return;
  1308. if (status) {
  1309. handle_usb_error(status, __func__, serial->parent);
  1310. return;
  1311. }
  1312. tiocmget = serial->tiocmget;
  1313. if (!tiocmget)
  1314. return;
  1315. usb = serial->parent->usb;
  1316. serial_state_notification = &tiocmget->serial_state_notification;
  1317. if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE ||
  1318. serial_state_notification->bNotification != B_NOTIFICATION ||
  1319. le16_to_cpu(serial_state_notification->wValue) != W_VALUE ||
  1320. le16_to_cpu(serial_state_notification->wIndex) != W_INDEX ||
  1321. le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) {
  1322. dev_warn(&usb->dev,
  1323. "hso received invalid serial state notification\n");
  1324. DUMP(serial_state_notification,
  1325. sizeof(struct hso_serial_state_notification));
  1326. } else {
  1327. UART_state_bitmap = le16_to_cpu(serial_state_notification->
  1328. UART_state_bitmap);
  1329. prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap;
  1330. icount = &tiocmget->icount;
  1331. spin_lock(&serial->serial_lock);
  1332. if ((UART_state_bitmap & B_OVERRUN) !=
  1333. (prev_UART_state_bitmap & B_OVERRUN))
  1334. icount->parity++;
  1335. if ((UART_state_bitmap & B_PARITY) !=
  1336. (prev_UART_state_bitmap & B_PARITY))
  1337. icount->parity++;
  1338. if ((UART_state_bitmap & B_FRAMING) !=
  1339. (prev_UART_state_bitmap & B_FRAMING))
  1340. icount->frame++;
  1341. if ((UART_state_bitmap & B_RING_SIGNAL) &&
  1342. !(prev_UART_state_bitmap & B_RING_SIGNAL))
  1343. icount->rng++;
  1344. if ((UART_state_bitmap & B_BREAK) !=
  1345. (prev_UART_state_bitmap & B_BREAK))
  1346. icount->brk++;
  1347. if ((UART_state_bitmap & B_TX_CARRIER) !=
  1348. (prev_UART_state_bitmap & B_TX_CARRIER))
  1349. icount->dsr++;
  1350. if ((UART_state_bitmap & B_RX_CARRIER) !=
  1351. (prev_UART_state_bitmap & B_RX_CARRIER))
  1352. icount->dcd++;
  1353. tiocmget->prev_UART_state_bitmap = UART_state_bitmap;
  1354. spin_unlock(&serial->serial_lock);
  1355. tiocmget->intr_completed = 1;
  1356. wake_up_interruptible(&tiocmget->waitq);
  1357. }
  1358. memset(serial_state_notification, 0,
  1359. sizeof(struct hso_serial_state_notification));
  1360. tiocmget_submit_urb(serial,
  1361. tiocmget,
  1362. serial->parent->usb);
  1363. }
  1364. /*
  1365. * next few functions largely stolen from drivers/serial/serial_core.c
  1366. */
  1367. /* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1368. * - mask passed in arg for lines of interest
  1369. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1370. * Caller should use TIOCGICOUNT to see which one it was
  1371. */
  1372. static int
  1373. hso_wait_modem_status(struct hso_serial *serial, unsigned long arg)
  1374. {
  1375. DECLARE_WAITQUEUE(wait, current);
  1376. struct uart_icount cprev, cnow;
  1377. struct hso_tiocmget *tiocmget;
  1378. int ret;
  1379. tiocmget = serial->tiocmget;
  1380. if (!tiocmget)
  1381. return -ENOENT;
  1382. /*
  1383. * note the counters on entry
  1384. */
  1385. spin_lock_irq(&serial->serial_lock);
  1386. memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount));
  1387. spin_unlock_irq(&serial->serial_lock);
  1388. add_wait_queue(&tiocmget->waitq, &wait);
  1389. for (;;) {
  1390. spin_lock_irq(&serial->serial_lock);
  1391. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1392. spin_unlock_irq(&serial->serial_lock);
  1393. set_current_state(TASK_INTERRUPTIBLE);
  1394. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1395. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1396. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd))) {
  1397. ret = 0;
  1398. break;
  1399. }
  1400. schedule();
  1401. /* see if a signal did it */
  1402. if (signal_pending(current)) {
  1403. ret = -ERESTARTSYS;
  1404. break;
  1405. }
  1406. cprev = cnow;
  1407. }
  1408. current->state = TASK_RUNNING;
  1409. remove_wait_queue(&tiocmget->waitq, &wait);
  1410. return ret;
  1411. }
  1412. /*
  1413. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1414. * Return: write counters to the user passed counter struct
  1415. * NB: both 1->0 and 0->1 transitions are counted except for
  1416. * RI where only 0->1 is counted.
  1417. */
  1418. static int hso_get_count(struct hso_serial *serial,
  1419. struct serial_icounter_struct __user *icnt)
  1420. {
  1421. struct serial_icounter_struct icount;
  1422. struct uart_icount cnow;
  1423. struct hso_tiocmget *tiocmget = serial->tiocmget;
  1424. if (!tiocmget)
  1425. return -ENOENT;
  1426. spin_lock_irq(&serial->serial_lock);
  1427. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1428. spin_unlock_irq(&serial->serial_lock);
  1429. icount.cts = cnow.cts;
  1430. icount.dsr = cnow.dsr;
  1431. icount.rng = cnow.rng;
  1432. icount.dcd = cnow.dcd;
  1433. icount.rx = cnow.rx;
  1434. icount.tx = cnow.tx;
  1435. icount.frame = cnow.frame;
  1436. icount.overrun = cnow.overrun;
  1437. icount.parity = cnow.parity;
  1438. icount.brk = cnow.brk;
  1439. icount.buf_overrun = cnow.buf_overrun;
  1440. return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
  1441. }
  1442. static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
  1443. {
  1444. int retval;
  1445. struct hso_serial *serial = get_serial_by_tty(tty);
  1446. struct hso_tiocmget *tiocmget;
  1447. u16 UART_state_bitmap;
  1448. /* sanity check */
  1449. if (!serial) {
  1450. D1("no tty structures");
  1451. return -EINVAL;
  1452. }
  1453. spin_lock_irq(&serial->serial_lock);
  1454. retval = ((serial->rts_state) ? TIOCM_RTS : 0) |
  1455. ((serial->dtr_state) ? TIOCM_DTR : 0);
  1456. tiocmget = serial->tiocmget;
  1457. if (tiocmget) {
  1458. UART_state_bitmap = le16_to_cpu(
  1459. tiocmget->prev_UART_state_bitmap);
  1460. if (UART_state_bitmap & B_RING_SIGNAL)
  1461. retval |= TIOCM_RNG;
  1462. if (UART_state_bitmap & B_RX_CARRIER)
  1463. retval |= TIOCM_CD;
  1464. if (UART_state_bitmap & B_TX_CARRIER)
  1465. retval |= TIOCM_DSR;
  1466. }
  1467. spin_unlock_irq(&serial->serial_lock);
  1468. return retval;
  1469. }
  1470. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  1471. unsigned int set, unsigned int clear)
  1472. {
  1473. int val = 0;
  1474. unsigned long flags;
  1475. int if_num;
  1476. struct hso_serial *serial = get_serial_by_tty(tty);
  1477. /* sanity check */
  1478. if (!serial) {
  1479. D1("no tty structures");
  1480. return -EINVAL;
  1481. }
  1482. if ((serial->parent->port_spec & HSO_PORT_MASK) != HSO_PORT_MODEM)
  1483. return -EINVAL;
  1484. if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
  1485. spin_lock_irqsave(&serial->serial_lock, flags);
  1486. if (set & TIOCM_RTS)
  1487. serial->rts_state = 1;
  1488. if (set & TIOCM_DTR)
  1489. serial->dtr_state = 1;
  1490. if (clear & TIOCM_RTS)
  1491. serial->rts_state = 0;
  1492. if (clear & TIOCM_DTR)
  1493. serial->dtr_state = 0;
  1494. if (serial->dtr_state)
  1495. val |= 0x01;
  1496. if (serial->rts_state)
  1497. val |= 0x02;
  1498. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1499. return usb_control_msg(serial->parent->usb,
  1500. usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
  1501. 0x21, val, if_num, NULL, 0,
  1502. USB_CTRL_SET_TIMEOUT);
  1503. }
  1504. static int hso_serial_ioctl(struct tty_struct *tty, struct file *file,
  1505. unsigned int cmd, unsigned long arg)
  1506. {
  1507. struct hso_serial *serial = get_serial_by_tty(tty);
  1508. void __user *uarg = (void __user *)arg;
  1509. int ret = 0;
  1510. D4("IOCTL cmd: %d, arg: %ld", cmd, arg);
  1511. if (!serial)
  1512. return -ENODEV;
  1513. switch (cmd) {
  1514. case TIOCMIWAIT:
  1515. ret = hso_wait_modem_status(serial, arg);
  1516. break;
  1517. case TIOCGICOUNT:
  1518. ret = hso_get_count(serial, uarg);
  1519. break;
  1520. default:
  1521. ret = -ENOIOCTLCMD;
  1522. break;
  1523. }
  1524. return ret;
  1525. }
  1526. /* starts a transmit */
  1527. static void hso_kick_transmit(struct hso_serial *serial)
  1528. {
  1529. u8 *temp;
  1530. unsigned long flags;
  1531. int res;
  1532. spin_lock_irqsave(&serial->serial_lock, flags);
  1533. if (!serial->tx_buffer_count)
  1534. goto out;
  1535. if (serial->tx_urb_used)
  1536. goto out;
  1537. /* Wakeup USB interface if necessary */
  1538. if (hso_get_activity(serial->parent) == -EAGAIN)
  1539. goto out;
  1540. /* Switch pointers around to avoid memcpy */
  1541. temp = serial->tx_buffer;
  1542. serial->tx_buffer = serial->tx_data;
  1543. serial->tx_data = temp;
  1544. serial->tx_data_count = serial->tx_buffer_count;
  1545. serial->tx_buffer_count = 0;
  1546. /* If temp is set, it means we switched buffers */
  1547. if (temp && serial->write_data) {
  1548. res = serial->write_data(serial);
  1549. if (res >= 0)
  1550. serial->tx_urb_used = 1;
  1551. }
  1552. out:
  1553. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1554. }
  1555. /* make a request (for reading and writing data to muxed serial port) */
  1556. static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
  1557. struct urb *ctrl_urb,
  1558. struct usb_ctrlrequest *ctrl_req,
  1559. u8 *ctrl_urb_data, u32 size)
  1560. {
  1561. int result;
  1562. int pipe;
  1563. /* Sanity check */
  1564. if (!serial || !ctrl_urb || !ctrl_req) {
  1565. printk(KERN_ERR "%s: Wrong arguments\n", __func__);
  1566. return -EINVAL;
  1567. }
  1568. /* initialize */
  1569. ctrl_req->wValue = 0;
  1570. ctrl_req->wIndex = cpu_to_le16(hso_port_to_mux(port));
  1571. ctrl_req->wLength = cpu_to_le16(size);
  1572. if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
  1573. /* Reading command */
  1574. ctrl_req->bRequestType = USB_DIR_IN |
  1575. USB_TYPE_OPTION_VENDOR |
  1576. USB_RECIP_INTERFACE;
  1577. ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  1578. pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
  1579. } else {
  1580. /* Writing command */
  1581. ctrl_req->bRequestType = USB_DIR_OUT |
  1582. USB_TYPE_OPTION_VENDOR |
  1583. USB_RECIP_INTERFACE;
  1584. ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  1585. pipe = usb_sndctrlpipe(serial->parent->usb, 0);
  1586. }
  1587. /* syslog */
  1588. D2("%s command (%02x) len: %d, port: %d",
  1589. type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
  1590. ctrl_req->bRequestType, ctrl_req->wLength, port);
  1591. /* Load ctrl urb */
  1592. ctrl_urb->transfer_flags = 0;
  1593. usb_fill_control_urb(ctrl_urb,
  1594. serial->parent->usb,
  1595. pipe,
  1596. (u8 *) ctrl_req,
  1597. ctrl_urb_data, size, ctrl_callback, serial);
  1598. /* Send it on merry way */
  1599. result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
  1600. if (result) {
  1601. dev_err(&ctrl_urb->dev->dev,
  1602. "%s failed submit ctrl_urb %d type %d\n", __func__,
  1603. result, type);
  1604. return result;
  1605. }
  1606. /* done */
  1607. return size;
  1608. }
  1609. /* called by intr_callback when read occurs */
  1610. static int hso_mux_serial_read(struct hso_serial *serial)
  1611. {
  1612. if (!serial)
  1613. return -EINVAL;
  1614. /* clean data */
  1615. memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
  1616. /* make the request */
  1617. if (serial->num_rx_urbs != 1) {
  1618. dev_err(&serial->parent->interface->dev,
  1619. "ERROR: mux'd reads with multiple buffers "
  1620. "not possible\n");
  1621. return 0;
  1622. }
  1623. return mux_device_request(serial,
  1624. USB_CDC_GET_ENCAPSULATED_RESPONSE,
  1625. serial->parent->port_spec & HSO_PORT_MASK,
  1626. serial->rx_urb[0],
  1627. &serial->ctrl_req_rx,
  1628. serial->rx_data[0], serial->rx_data_length);
  1629. }
  1630. /* used for muxed serial port callback (muxed serial read) */
  1631. static void intr_callback(struct urb *urb)
  1632. {
  1633. struct hso_shared_int *shared_int = urb->context;
  1634. struct hso_serial *serial;
  1635. unsigned char *port_req;
  1636. int status = urb->status;
  1637. int i;
  1638. usb_mark_last_busy(urb->dev);
  1639. /* sanity check */
  1640. if (!shared_int)
  1641. return;
  1642. /* status check */
  1643. if (status) {
  1644. handle_usb_error(status, __func__, NULL);
  1645. return;
  1646. }
  1647. D4("\n--- Got intr callback 0x%02X ---", status);
  1648. /* what request? */
  1649. port_req = urb->transfer_buffer;
  1650. D4(" port_req = 0x%.2X\n", *port_req);
  1651. /* loop over all muxed ports to find the one sending this */
  1652. for (i = 0; i < 8; i++) {
  1653. /* max 8 channels on MUX */
  1654. if (*port_req & (1 << i)) {
  1655. serial = get_serial_by_shared_int_and_type(shared_int,
  1656. (1 << i));
  1657. if (serial != NULL) {
  1658. D1("Pending read interrupt on port %d\n", i);
  1659. spin_lock(&serial->serial_lock);
  1660. if (serial->rx_state == RX_IDLE &&
  1661. serial->open_count > 0) {
  1662. /* Setup and send a ctrl req read on
  1663. * port i */
  1664. if (!serial->rx_urb_filled[0]) {
  1665. serial->rx_state = RX_SENT;
  1666. hso_mux_serial_read(serial);
  1667. } else
  1668. serial->rx_state = RX_PENDING;
  1669. } else {
  1670. D1("Already a read pending on "
  1671. "port %d or port not open\n", i);
  1672. }
  1673. spin_unlock(&serial->serial_lock);
  1674. }
  1675. }
  1676. }
  1677. /* Resubmit interrupt urb */
  1678. hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
  1679. }
  1680. /* called for writing to muxed serial port */
  1681. static int hso_mux_serial_write_data(struct hso_serial *serial)
  1682. {
  1683. if (NULL == serial)
  1684. return -EINVAL;
  1685. return mux_device_request(serial,
  1686. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  1687. serial->parent->port_spec & HSO_PORT_MASK,
  1688. serial->tx_urb,
  1689. &serial->ctrl_req_tx,
  1690. serial->tx_data, serial->tx_data_count);
  1691. }
  1692. /* write callback for Diag and CS port */
  1693. static void hso_std_serial_write_bulk_callback(struct urb *urb)
  1694. {
  1695. struct hso_serial *serial = urb->context;
  1696. int status = urb->status;
  1697. struct tty_struct *tty;
  1698. /* sanity check */
  1699. if (!serial) {
  1700. D1("serial == NULL");
  1701. return;
  1702. }
  1703. spin_lock(&serial->serial_lock);
  1704. serial->tx_urb_used = 0;
  1705. tty = tty_kref_get(serial->tty);
  1706. spin_unlock(&serial->serial_lock);
  1707. if (status) {
  1708. handle_usb_error(status, __func__, serial->parent);
  1709. tty_kref_put(tty);
  1710. return;
  1711. }
  1712. hso_put_activity(serial->parent);
  1713. if (tty) {
  1714. tty_wakeup(tty);
  1715. tty_kref_put(tty);
  1716. }
  1717. hso_kick_transmit(serial);
  1718. D1(" ");
  1719. }
  1720. /* called for writing diag or CS serial port */
  1721. static int hso_std_serial_write_data(struct hso_serial *serial)
  1722. {
  1723. int count = serial->tx_data_count;
  1724. int result;
  1725. usb_fill_bulk_urb(serial->tx_urb,
  1726. serial->parent->usb,
  1727. usb_sndbulkpipe(serial->parent->usb,
  1728. serial->out_endp->
  1729. bEndpointAddress & 0x7F),
  1730. serial->tx_data, serial->tx_data_count,
  1731. hso_std_serial_write_bulk_callback, serial);
  1732. result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
  1733. if (result) {
  1734. dev_warn(&serial->parent->usb->dev,
  1735. "Failed to submit urb - res %d\n", result);
  1736. return result;
  1737. }
  1738. return count;
  1739. }
  1740. /* callback after read or write on muxed serial port */
  1741. static void ctrl_callback(struct urb *urb)
  1742. {
  1743. struct hso_serial *serial = urb->context;
  1744. struct usb_ctrlrequest *req;
  1745. int status = urb->status;
  1746. struct tty_struct *tty;
  1747. /* sanity check */
  1748. if (!serial)
  1749. return;
  1750. spin_lock(&serial->serial_lock);
  1751. serial->tx_urb_used = 0;
  1752. tty = tty_kref_get(serial->tty);
  1753. spin_unlock(&serial->serial_lock);
  1754. if (status) {
  1755. handle_usb_error(status, __func__, serial->parent);
  1756. tty_kref_put(tty);
  1757. return;
  1758. }
  1759. /* what request? */
  1760. req = (struct usb_ctrlrequest *)(urb->setup_packet);
  1761. D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
  1762. D4("Actual length of urb = %d\n", urb->actual_length);
  1763. DUMP1(urb->transfer_buffer, urb->actual_length);
  1764. if (req->bRequestType ==
  1765. (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
  1766. /* response to a read command */
  1767. serial->rx_urb_filled[0] = 1;
  1768. spin_lock(&serial->serial_lock);
  1769. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1770. spin_unlock(&serial->serial_lock);
  1771. } else {
  1772. hso_put_activity(serial->parent);
  1773. if (tty)
  1774. tty_wakeup(tty);
  1775. /* response to a write command */
  1776. hso_kick_transmit(serial);
  1777. }
  1778. tty_kref_put(tty);
  1779. }
  1780. /* handle RX data for serial port */
  1781. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
  1782. {
  1783. struct tty_struct *tty;
  1784. int write_length_remaining = 0;
  1785. int curr_write_len;
  1786. /* Sanity check */
  1787. if (urb == NULL || serial == NULL) {
  1788. D1("serial = NULL");
  1789. return -2;
  1790. }
  1791. /* All callers to put_rxbuf_data hold serial_lock */
  1792. tty = tty_kref_get(serial->tty);
  1793. /* Push data to tty */
  1794. if (tty) {
  1795. write_length_remaining = urb->actual_length -
  1796. serial->curr_rx_urb_offset;
  1797. D1("data to push to tty");
  1798. while (write_length_remaining) {
  1799. if (test_bit(TTY_THROTTLED, &tty->flags)) {
  1800. tty_kref_put(tty);
  1801. return -1;
  1802. }
  1803. curr_write_len = tty_insert_flip_string
  1804. (tty, urb->transfer_buffer +
  1805. serial->curr_rx_urb_offset,
  1806. write_length_remaining);
  1807. serial->curr_rx_urb_offset += curr_write_len;
  1808. write_length_remaining -= curr_write_len;
  1809. tty_flip_buffer_push(tty);
  1810. }
  1811. }
  1812. if (write_length_remaining == 0) {
  1813. serial->curr_rx_urb_offset = 0;
  1814. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1815. }
  1816. tty_kref_put(tty);
  1817. return write_length_remaining;
  1818. }
  1819. /* Base driver functions */
  1820. static void hso_log_port(struct hso_device *hso_dev)
  1821. {
  1822. char *port_type;
  1823. char port_dev[20];
  1824. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  1825. case HSO_PORT_CONTROL:
  1826. port_type = "Control";
  1827. break;
  1828. case HSO_PORT_APP:
  1829. port_type = "Application";
  1830. break;
  1831. case HSO_PORT_GPS:
  1832. port_type = "GPS";
  1833. break;
  1834. case HSO_PORT_GPS_CONTROL:
  1835. port_type = "GPS control";
  1836. break;
  1837. case HSO_PORT_APP2:
  1838. port_type = "Application2";
  1839. break;
  1840. case HSO_PORT_PCSC:
  1841. port_type = "PCSC";
  1842. break;
  1843. case HSO_PORT_DIAG:
  1844. port_type = "Diagnostic";
  1845. break;
  1846. case HSO_PORT_DIAG2:
  1847. port_type = "Diagnostic2";
  1848. break;
  1849. case HSO_PORT_MODEM:
  1850. port_type = "Modem";
  1851. break;
  1852. case HSO_PORT_NETWORK:
  1853. port_type = "Network";
  1854. break;
  1855. default:
  1856. port_type = "Unknown";
  1857. break;
  1858. }
  1859. if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  1860. sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
  1861. } else
  1862. sprintf(port_dev, "/dev/%s%d", tty_filename,
  1863. dev2ser(hso_dev)->minor);
  1864. dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
  1865. port_type, port_dev);
  1866. }
  1867. static int hso_start_net_device(struct hso_device *hso_dev)
  1868. {
  1869. int i, result = 0;
  1870. struct hso_net *hso_net = dev2net(hso_dev);
  1871. if (!hso_net)
  1872. return -ENODEV;
  1873. /* send URBs for all read buffers */
  1874. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1875. /* Prep a receive URB */
  1876. usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1877. hso_dev->usb,
  1878. usb_rcvbulkpipe(hso_dev->usb,
  1879. hso_net->in_endp->
  1880. bEndpointAddress & 0x7F),
  1881. hso_net->mux_bulk_rx_buf_pool[i],
  1882. MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
  1883. hso_net);
  1884. /* Put it out there so the device can send us stuff */
  1885. result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1886. GFP_NOIO);
  1887. if (result)
  1888. dev_warn(&hso_dev->usb->dev,
  1889. "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
  1890. i, result);
  1891. }
  1892. return result;
  1893. }
  1894. static int hso_stop_net_device(struct hso_device *hso_dev)
  1895. {
  1896. int i;
  1897. struct hso_net *hso_net = dev2net(hso_dev);
  1898. if (!hso_net)
  1899. return -ENODEV;
  1900. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1901. if (hso_net->mux_bulk_rx_urb_pool[i])
  1902. usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  1903. }
  1904. if (hso_net->mux_bulk_tx_urb)
  1905. usb_kill_urb(hso_net->mux_bulk_tx_urb);
  1906. return 0;
  1907. }
  1908. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
  1909. {
  1910. int i, result = 0;
  1911. struct hso_serial *serial = dev2ser(hso_dev);
  1912. if (!serial)
  1913. return -ENODEV;
  1914. /* If it is not the MUX port fill in and submit a bulk urb (already
  1915. * allocated in hso_serial_start) */
  1916. if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
  1917. for (i = 0; i < serial->num_rx_urbs; i++) {
  1918. usb_fill_bulk_urb(serial->rx_urb[i],
  1919. serial->parent->usb,
  1920. usb_rcvbulkpipe(serial->parent->usb,
  1921. serial->in_endp->
  1922. bEndpointAddress &
  1923. 0x7F),
  1924. serial->rx_data[i],
  1925. serial->rx_data_length,
  1926. hso_std_serial_read_bulk_callback,
  1927. serial);
  1928. result = usb_submit_urb(serial->rx_urb[i], flags);
  1929. if (result) {
  1930. dev_warn(&serial->parent->usb->dev,
  1931. "Failed to submit urb - res %d\n",
  1932. result);
  1933. break;
  1934. }
  1935. }
  1936. } else {
  1937. mutex_lock(&serial->shared_int->shared_int_lock);
  1938. if (!serial->shared_int->use_count) {
  1939. result =
  1940. hso_mux_submit_intr_urb(serial->shared_int,
  1941. hso_dev->usb, flags);
  1942. }
  1943. serial->shared_int->use_count++;
  1944. mutex_unlock(&serial->shared_int->shared_int_lock);
  1945. }
  1946. if (serial->tiocmget)
  1947. tiocmget_submit_urb(serial,
  1948. serial->tiocmget,
  1949. serial->parent->usb);
  1950. return result;
  1951. }
  1952. static int hso_stop_serial_device(struct hso_device *hso_dev)
  1953. {
  1954. int i;
  1955. struct hso_serial *serial = dev2ser(hso_dev);
  1956. struct hso_tiocmget *tiocmget;
  1957. if (!serial)
  1958. return -ENODEV;
  1959. for (i = 0; i < serial->num_rx_urbs; i++) {
  1960. if (serial->rx_urb[i]) {
  1961. usb_kill_urb(serial->rx_urb[i]);
  1962. serial->rx_urb_filled[i] = 0;
  1963. }
  1964. }
  1965. serial->curr_rx_urb_idx = 0;
  1966. serial->curr_rx_urb_offset = 0;
  1967. if (serial->tx_urb)
  1968. usb_kill_urb(serial->tx_urb);
  1969. if (serial->shared_int) {
  1970. mutex_lock(&serial->shared_int->shared_int_lock);
  1971. if (serial->shared_int->use_count &&
  1972. (--serial->shared_int->use_count == 0)) {
  1973. struct urb *urb;
  1974. urb = serial->shared_int->shared_intr_urb;
  1975. if (urb)
  1976. usb_kill_urb(urb);
  1977. }
  1978. mutex_unlock(&serial->shared_int->shared_int_lock);
  1979. }
  1980. tiocmget = serial->tiocmget;
  1981. if (tiocmget) {
  1982. wake_up_interruptible(&tiocmget->waitq);
  1983. usb_kill_urb(tiocmget->urb);
  1984. }
  1985. return 0;
  1986. }
  1987. static void hso_serial_common_free(struct hso_serial *serial)
  1988. {
  1989. int i;
  1990. if (serial->parent->dev)
  1991. device_remove_file(serial->parent->dev, &dev_attr_hsotype);
  1992. tty_unregister_device(tty_drv, serial->minor);
  1993. for (i = 0; i < serial->num_rx_urbs; i++) {
  1994. /* unlink and free RX URB */
  1995. usb_free_urb(serial->rx_urb[i]);
  1996. /* free the RX buffer */
  1997. kfree(serial->rx_data[i]);
  1998. }
  1999. /* unlink and free TX URB */
  2000. usb_free_urb(serial->tx_urb);
  2001. kfree(serial->tx_data);
  2002. }
  2003. static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
  2004. int rx_size, int tx_size)
  2005. {
  2006. struct device *dev;
  2007. int minor;
  2008. int i;
  2009. minor = get_free_serial_index();
  2010. if (minor < 0)
  2011. goto exit;
  2012. /* register our minor number */
  2013. serial->parent->dev = tty_register_device(tty_drv, minor,
  2014. &serial->parent->interface->dev);
  2015. dev = serial->parent->dev;
  2016. dev_set_drvdata(dev, serial->parent);
  2017. i = device_create_file(dev, &dev_attr_hsotype);
  2018. /* fill in specific data for later use */
  2019. serial->minor = minor;
  2020. serial->magic = HSO_SERIAL_MAGIC;
  2021. spin_lock_init(&serial->serial_lock);
  2022. serial->num_rx_urbs = num_urbs;
  2023. /* RX, allocate urb and initialize */
  2024. /* prepare our RX buffer */
  2025. serial->rx_data_length = rx_size;
  2026. for (i = 0; i < serial->num_rx_urbs; i++) {
  2027. serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
  2028. if (!serial->rx_urb[i]) {
  2029. dev_err(dev, "Could not allocate urb?\n");
  2030. goto exit;
  2031. }
  2032. serial->rx_urb[i]->transfer_buffer = NULL;
  2033. serial->rx_urb[i]->transfer_buffer_length = 0;
  2034. serial->rx_data[i] = kzalloc(serial->rx_data_length,
  2035. GFP_KERNEL);
  2036. if (!serial->rx_data[i]) {
  2037. dev_err(dev, "%s - Out of memory\n", __func__);
  2038. goto exit;
  2039. }
  2040. }
  2041. /* TX, allocate urb and initialize */
  2042. serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2043. if (!serial->tx_urb) {
  2044. dev_err(dev, "Could not allocate urb?\n");
  2045. goto exit;
  2046. }
  2047. serial->tx_urb->transfer_buffer = NULL;
  2048. serial->tx_urb->transfer_buffer_length = 0;
  2049. /* prepare our TX buffer */
  2050. serial->tx_data_count = 0;
  2051. serial->tx_buffer_count = 0;
  2052. serial->tx_data_length = tx_size;
  2053. serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
  2054. if (!serial->tx_data) {
  2055. dev_err(dev, "%s - Out of memory\n", __func__);
  2056. goto exit;
  2057. }
  2058. serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
  2059. if (!serial->tx_buffer) {
  2060. dev_err(dev, "%s - Out of memory\n", __func__);
  2061. goto exit;
  2062. }
  2063. return 0;
  2064. exit:
  2065. hso_serial_common_free(serial);
  2066. return -1;
  2067. }
  2068. /* Creates a general hso device */
  2069. static struct hso_device *hso_create_device(struct usb_interface *intf,
  2070. int port_spec)
  2071. {
  2072. struct hso_device *hso_dev;
  2073. hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
  2074. if (!hso_dev)
  2075. return NULL;
  2076. hso_dev->port_spec = port_spec;
  2077. hso_dev->usb = interface_to_usbdev(intf);
  2078. hso_dev->interface = intf;
  2079. kref_init(&hso_dev->ref);
  2080. mutex_init(&hso_dev->mutex);
  2081. INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
  2082. INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
  2083. INIT_WORK(&hso_dev->reset_device, reset_device);
  2084. return hso_dev;
  2085. }
  2086. /* Removes a network device in the network device table */
  2087. static int remove_net_device(struct hso_device *hso_dev)
  2088. {
  2089. int i;
  2090. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2091. if (network_table[i] == hso_dev) {
  2092. network_table[i] = NULL;
  2093. break;
  2094. }
  2095. }
  2096. if (i == HSO_MAX_NET_DEVICES)
  2097. return -1;
  2098. return 0;
  2099. }
  2100. /* Frees our network device */
  2101. static void hso_free_net_device(struct hso_device *hso_dev)
  2102. {
  2103. int i;
  2104. struct hso_net *hso_net = dev2net(hso_dev);
  2105. if (!hso_net)
  2106. return;
  2107. remove_net_device(hso_net->parent);
  2108. if (hso_net->net) {
  2109. unregister_netdev(hso_net->net);
  2110. free_netdev(hso_net->net);
  2111. }
  2112. /* start freeing */
  2113. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2114. usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  2115. kfree(hso_net->mux_bulk_rx_buf_pool[i]);
  2116. hso_net->mux_bulk_rx_buf_pool[i] = NULL;
  2117. }
  2118. usb_free_urb(hso_net->mux_bulk_tx_urb);
  2119. kfree(hso_net->mux_bulk_tx_buf);
  2120. hso_net->mux_bulk_tx_buf = NULL;
  2121. kfree(hso_dev);
  2122. }
  2123. static const struct net_device_ops hso_netdev_ops = {
  2124. .ndo_open = hso_net_open,
  2125. .ndo_stop = hso_net_close,
  2126. .ndo_start_xmit = hso_net_start_xmit,
  2127. .ndo_tx_timeout = hso_net_tx_timeout,
  2128. };
  2129. /* initialize the network interface */
  2130. static void hso_net_init(struct net_device *net)
  2131. {
  2132. struct hso_net *hso_net = netdev_priv(net);
  2133. D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
  2134. /* fill in the other fields */
  2135. net->netdev_ops = &hso_netdev_ops;
  2136. net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
  2137. net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  2138. net->type = ARPHRD_NONE;
  2139. net->mtu = DEFAULT_MTU - 14;
  2140. net->tx_queue_len = 10;
  2141. SET_ETHTOOL_OPS(net, &ops);
  2142. /* and initialize the semaphore */
  2143. spin_lock_init(&hso_net->net_lock);
  2144. }
  2145. /* Adds a network device in the network device table */
  2146. static int add_net_device(struct hso_device *hso_dev)
  2147. {
  2148. int i;
  2149. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2150. if (network_table[i] == NULL) {
  2151. network_table[i] = hso_dev;
  2152. break;
  2153. }
  2154. }
  2155. if (i == HSO_MAX_NET_DEVICES)
  2156. return -1;
  2157. return 0;
  2158. }
  2159. static int hso_rfkill_set_block(void *data, bool blocked)
  2160. {
  2161. struct hso_device *hso_dev = data;
  2162. int enabled = !blocked;
  2163. int rv;
  2164. mutex_lock(&hso_dev->mutex);
  2165. if (hso_dev->usb_gone)
  2166. rv = 0;
  2167. else
  2168. rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
  2169. enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
  2170. USB_CTRL_SET_TIMEOUT);
  2171. mutex_unlock(&hso_dev->mutex);
  2172. return rv;
  2173. }
  2174. static const struct rfkill_ops hso_rfkill_ops = {
  2175. .set_block = hso_rfkill_set_block,
  2176. };
  2177. /* Creates and sets up everything for rfkill */
  2178. static void hso_create_rfkill(struct hso_device *hso_dev,
  2179. struct usb_interface *interface)
  2180. {
  2181. struct hso_net *hso_net = dev2net(hso_dev);
  2182. struct device *dev = &hso_net->net->dev;
  2183. char *rfkn;
  2184. rfkn = kzalloc(20, GFP_KERNEL);
  2185. if (!rfkn)
  2186. dev_err(dev, "%s - Out of memory\n", __func__);
  2187. snprintf(rfkn, 20, "hso-%d",
  2188. interface->altsetting->desc.bInterfaceNumber);
  2189. hso_net->rfkill = rfkill_alloc(rfkn,
  2190. &interface_to_usbdev(interface)->dev,
  2191. RFKILL_TYPE_WWAN,
  2192. &hso_rfkill_ops, hso_dev);
  2193. if (!hso_net->rfkill) {
  2194. dev_err(dev, "%s - Out of memory\n", __func__);
  2195. kfree(rfkn);
  2196. return;
  2197. }
  2198. if (rfkill_register(hso_net->rfkill) < 0) {
  2199. rfkill_destroy(hso_net->rfkill);
  2200. kfree(rfkn);
  2201. hso_net->rfkill = NULL;
  2202. dev_err(dev, "%s - Failed to register rfkill\n", __func__);
  2203. return;
  2204. }
  2205. }
  2206. static struct device_type hso_type = {
  2207. .name = "wwan",
  2208. };
  2209. /* Creates our network device */
  2210. static struct hso_device *hso_create_net_device(struct usb_interface *interface,
  2211. int port_spec)
  2212. {
  2213. int result, i;
  2214. struct net_device *net;
  2215. struct hso_net *hso_net;
  2216. struct hso_device *hso_dev;
  2217. hso_dev = hso_create_device(interface, port_spec);
  2218. if (!hso_dev)
  2219. return NULL;
  2220. /* allocate our network device, then we can put in our private data */
  2221. /* call hso_net_init to do the basic initialization */
  2222. net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
  2223. if (!net) {
  2224. dev_err(&interface->dev, "Unable to create ethernet device\n");
  2225. goto exit;
  2226. }
  2227. hso_net = netdev_priv(net);
  2228. hso_dev->port_data.dev_net = hso_net;
  2229. hso_net->net = net;
  2230. hso_net->parent = hso_dev;
  2231. hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2232. USB_DIR_IN);
  2233. if (!hso_net->in_endp) {
  2234. dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
  2235. goto exit;
  2236. }
  2237. hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2238. USB_DIR_OUT);
  2239. if (!hso_net->out_endp) {
  2240. dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
  2241. goto exit;
  2242. }
  2243. SET_NETDEV_DEV(net, &interface->dev);
  2244. SET_NETDEV_DEVTYPE(net, &hso_type);
  2245. /* registering our net device */
  2246. result = register_netdev(net);
  2247. if (result) {
  2248. dev_err(&interface->dev, "Failed to register device\n");
  2249. goto exit;
  2250. }
  2251. /* start allocating */
  2252. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2253. hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
  2254. if (!hso_net->mux_bulk_rx_urb_pool[i]) {
  2255. dev_err(&interface->dev, "Could not allocate rx urb\n");
  2256. goto exit;
  2257. }
  2258. hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
  2259. GFP_KERNEL);
  2260. if (!hso_net->mux_bulk_rx_buf_pool[i]) {
  2261. dev_err(&interface->dev, "Could not allocate rx buf\n");
  2262. goto exit;
  2263. }
  2264. }
  2265. hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2266. if (!hso_net->mux_bulk_tx_urb) {
  2267. dev_err(&interface->dev, "Could not allocate tx urb\n");
  2268. goto exit;
  2269. }
  2270. hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
  2271. if (!hso_net->mux_bulk_tx_buf) {
  2272. dev_err(&interface->dev, "Could not allocate tx buf\n");
  2273. goto exit;
  2274. }
  2275. add_net_device(hso_dev);
  2276. hso_log_port(hso_dev);
  2277. hso_create_rfkill(hso_dev, interface);
  2278. return hso_dev;
  2279. exit:
  2280. hso_free_net_device(hso_dev);
  2281. return NULL;
  2282. }
  2283. static void hso_free_tiomget(struct hso_serial *serial)
  2284. {
  2285. struct hso_tiocmget *tiocmget = serial->tiocmget;
  2286. if (tiocmget) {
  2287. if (tiocmget->urb) {
  2288. usb_free_urb(tiocmget->urb);
  2289. tiocmget->urb = NULL;
  2290. }
  2291. serial->tiocmget = NULL;
  2292. kfree(tiocmget);
  2293. }
  2294. }
  2295. /* Frees an AT channel ( goes for both mux and non-mux ) */
  2296. static void hso_free_serial_device(struct hso_device *hso_dev)
  2297. {
  2298. struct hso_serial *serial = dev2ser(hso_dev);
  2299. if (!serial)
  2300. return;
  2301. set_serial_by_index(serial->minor, NULL);
  2302. hso_serial_common_free(serial);
  2303. if (serial->shared_int) {
  2304. mutex_lock(&serial->shared_int->shared_int_lock);
  2305. if (--serial->shared_int->ref_count == 0)
  2306. hso_free_shared_int(serial->shared_int);
  2307. else
  2308. mutex_unlock(&serial->shared_int->shared_int_lock);
  2309. }
  2310. hso_free_tiomget(serial);
  2311. kfree(serial);
  2312. kfree(hso_dev);
  2313. }
  2314. /* Creates a bulk AT channel */
  2315. static struct hso_device *hso_create_bulk_serial_device(
  2316. struct usb_interface *interface, int port)
  2317. {
  2318. struct hso_device *hso_dev;
  2319. struct hso_serial *serial;
  2320. int num_urbs;
  2321. struct hso_tiocmget *tiocmget;
  2322. hso_dev = hso_create_device(interface, port);
  2323. if (!hso_dev)
  2324. return NULL;
  2325. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2326. if (!serial)
  2327. goto exit;
  2328. serial->parent = hso_dev;
  2329. hso_dev->port_data.dev_serial = serial;
  2330. if ((port & HSO_PORT_MASK) == HSO_PORT_MODEM) {
  2331. num_urbs = 2;
  2332. serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget),
  2333. GFP_KERNEL);
  2334. /* it isn't going to break our heart if serial->tiocmget
  2335. * allocation fails don't bother checking this.
  2336. */
  2337. if (serial->tiocmget) {
  2338. tiocmget = serial->tiocmget;
  2339. tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
  2340. if (tiocmget->urb) {
  2341. mutex_init(&tiocmget->mutex);
  2342. init_waitqueue_head(&tiocmget->waitq);
  2343. tiocmget->endp = hso_get_ep(
  2344. interface,
  2345. USB_ENDPOINT_XFER_INT,
  2346. USB_DIR_IN);
  2347. } else
  2348. hso_free_tiomget(serial);
  2349. }
  2350. }
  2351. else
  2352. num_urbs = 1;
  2353. if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
  2354. BULK_URB_TX_SIZE))
  2355. goto exit;
  2356. serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2357. USB_DIR_IN);
  2358. if (!serial->in_endp) {
  2359. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2360. goto exit2;
  2361. }
  2362. if (!
  2363. (serial->out_endp =
  2364. hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
  2365. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2366. goto exit2;
  2367. }
  2368. serial->write_data = hso_std_serial_write_data;
  2369. /* and record this serial */
  2370. set_serial_by_index(serial->minor, serial);
  2371. /* setup the proc dirs and files if needed */
  2372. hso_log_port(hso_dev);
  2373. /* done, return it */
  2374. return hso_dev;
  2375. exit2:
  2376. hso_serial_common_free(serial);
  2377. exit:
  2378. hso_free_tiomget(serial);
  2379. kfree(serial);
  2380. kfree(hso_dev);
  2381. return NULL;
  2382. }
  2383. /* Creates a multiplexed AT channel */
  2384. static
  2385. struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
  2386. int port,
  2387. struct hso_shared_int *mux)
  2388. {
  2389. struct hso_device *hso_dev;
  2390. struct hso_serial *serial;
  2391. int port_spec;
  2392. port_spec = HSO_INTF_MUX;
  2393. port_spec &= ~HSO_PORT_MASK;
  2394. port_spec |= hso_mux_to_port(port);
  2395. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
  2396. return NULL;
  2397. hso_dev = hso_create_device(interface, port_spec);
  2398. if (!hso_dev)
  2399. return NULL;
  2400. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2401. if (!serial)
  2402. goto exit;
  2403. hso_dev->port_data.dev_serial = serial;
  2404. serial->parent = hso_dev;
  2405. if (hso_serial_common_create
  2406. (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
  2407. goto exit;
  2408. serial->tx_data_length--;
  2409. serial->write_data = hso_mux_serial_write_data;
  2410. serial->shared_int = mux;
  2411. mutex_lock(&serial->shared_int->shared_int_lock);
  2412. serial->shared_int->ref_count++;
  2413. mutex_unlock(&serial->shared_int->shared_int_lock);
  2414. /* and record this serial */
  2415. set_serial_by_index(serial->minor, serial);
  2416. /* setup the proc dirs and files if needed */
  2417. hso_log_port(hso_dev);
  2418. /* done, return it */
  2419. return hso_dev;
  2420. exit:
  2421. if (serial) {
  2422. tty_unregister_device(tty_drv, serial->minor);
  2423. kfree(serial);
  2424. }
  2425. if (hso_dev)
  2426. kfree(hso_dev);
  2427. return NULL;
  2428. }
  2429. static void hso_free_shared_int(struct hso_shared_int *mux)
  2430. {
  2431. usb_free_urb(mux->shared_intr_urb);
  2432. kfree(mux->shared_intr_buf);
  2433. mutex_unlock(&mux->shared_int_lock);
  2434. kfree(mux);
  2435. }
  2436. static
  2437. struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
  2438. {
  2439. struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  2440. if (!mux)
  2441. return NULL;
  2442. mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
  2443. USB_DIR_IN);
  2444. if (!mux->intr_endp) {
  2445. dev_err(&interface->dev, "Can't find INT IN endpoint\n");
  2446. goto exit;
  2447. }
  2448. mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  2449. if (!mux->shared_intr_urb) {
  2450. dev_err(&interface->dev, "Could not allocate intr urb?\n");
  2451. goto exit;
  2452. }
  2453. mux->shared_intr_buf =
  2454. kzalloc(le16_to_cpu(mux->intr_endp->wMaxPacketSize),
  2455. GFP_KERNEL);
  2456. if (!mux->shared_intr_buf) {
  2457. dev_err(&interface->dev, "Could not allocate intr buf?\n");
  2458. goto exit;
  2459. }
  2460. mutex_init(&mux->shared_int_lock);
  2461. return mux;
  2462. exit:
  2463. kfree(mux->shared_intr_buf);
  2464. usb_free_urb(mux->shared_intr_urb);
  2465. kfree(mux);
  2466. return NULL;
  2467. }
  2468. /* Gets the port spec for a certain interface */
  2469. static int hso_get_config_data(struct usb_interface *interface)
  2470. {
  2471. struct usb_device *usbdev = interface_to_usbdev(interface);
  2472. u8 config_data[17];
  2473. u32 if_num = interface->altsetting->desc.bInterfaceNumber;
  2474. s32 result;
  2475. if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
  2476. 0x86, 0xC0, 0, 0, config_data, 17,
  2477. USB_CTRL_SET_TIMEOUT) != 0x11) {
  2478. return -EIO;
  2479. }
  2480. switch (config_data[if_num]) {
  2481. case 0x0:
  2482. result = 0;
  2483. break;
  2484. case 0x1:
  2485. result = HSO_PORT_DIAG;
  2486. break;
  2487. case 0x2:
  2488. result = HSO_PORT_GPS;
  2489. break;
  2490. case 0x3:
  2491. result = HSO_PORT_GPS_CONTROL;
  2492. break;
  2493. case 0x4:
  2494. result = HSO_PORT_APP;
  2495. break;
  2496. case 0x5:
  2497. result = HSO_PORT_APP2;
  2498. break;
  2499. case 0x6:
  2500. result = HSO_PORT_CONTROL;
  2501. break;
  2502. case 0x7:
  2503. result = HSO_PORT_NETWORK;
  2504. break;
  2505. case 0x8:
  2506. result = HSO_PORT_MODEM;
  2507. break;
  2508. case 0x9:
  2509. result = HSO_PORT_MSD;
  2510. break;
  2511. case 0xa:
  2512. result = HSO_PORT_PCSC;
  2513. break;
  2514. case 0xb:
  2515. result = HSO_PORT_VOICE;
  2516. break;
  2517. default:
  2518. result = 0;
  2519. }
  2520. if (result)
  2521. result |= HSO_INTF_BULK;
  2522. if (config_data[16] & 0x1)
  2523. result |= HSO_INFO_CRC_BUG;
  2524. return result;
  2525. }
  2526. /* called once for each interface upon device insertion */
  2527. static int hso_probe(struct usb_interface *interface,
  2528. const struct usb_device_id *id)
  2529. {
  2530. int mux, i, if_num, port_spec;
  2531. unsigned char port_mask;
  2532. struct hso_device *hso_dev = NULL;
  2533. struct hso_shared_int *shared_int;
  2534. struct hso_device *tmp_dev = NULL;
  2535. if_num = interface->altsetting->desc.bInterfaceNumber;
  2536. /* Get the interface/port specification from either driver_info or from
  2537. * the device itself */
  2538. if (id->driver_info)
  2539. port_spec = ((u32 *)(id->driver_info))[if_num];
  2540. else
  2541. port_spec = hso_get_config_data(interface);
  2542. if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
  2543. dev_err(&interface->dev, "Not our interface\n");
  2544. return -ENODEV;
  2545. }
  2546. /* Check if we need to switch to alt interfaces prior to port
  2547. * configuration */
  2548. if (interface->num_altsetting > 1)
  2549. usb_set_interface(interface_to_usbdev(interface), if_num, 1);
  2550. interface->needs_remote_wakeup = 1;
  2551. /* Allocate new hso device(s) */
  2552. switch (port_spec & HSO_INTF_MASK) {
  2553. case HSO_INTF_MUX:
  2554. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  2555. /* Create the network device */
  2556. if (!disable_net) {
  2557. hso_dev = hso_create_net_device(interface,
  2558. port_spec);
  2559. if (!hso_dev)
  2560. goto exit;
  2561. tmp_dev = hso_dev;
  2562. }
  2563. }
  2564. if (hso_get_mux_ports(interface, &port_mask))
  2565. /* TODO: de-allocate everything */
  2566. goto exit;
  2567. shared_int = hso_create_shared_int(interface);
  2568. if (!shared_int)
  2569. goto exit;
  2570. for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
  2571. if (port_mask & i) {
  2572. hso_dev = hso_create_mux_serial_device(
  2573. interface, i, shared_int);
  2574. if (!hso_dev)
  2575. goto exit;
  2576. }
  2577. }
  2578. if (tmp_dev)
  2579. hso_dev = tmp_dev;
  2580. break;
  2581. case HSO_INTF_BULK:
  2582. /* It's a regular bulk interface */
  2583. if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) &&
  2584. !disable_net)
  2585. hso_dev = hso_create_net_device(interface, port_spec);
  2586. else
  2587. hso_dev =
  2588. hso_create_bulk_serial_device(interface, port_spec);
  2589. if (!hso_dev)
  2590. goto exit;
  2591. break;
  2592. default:
  2593. goto exit;
  2594. }
  2595. /* save our data pointer in this device */
  2596. usb_set_intfdata(interface, hso_dev);
  2597. /* done */
  2598. return 0;
  2599. exit:
  2600. hso_free_interface(interface);
  2601. return -ENODEV;
  2602. }
  2603. /* device removed, cleaning up */
  2604. static void hso_disconnect(struct usb_interface *interface)
  2605. {
  2606. hso_free_interface(interface);
  2607. /* remove reference of our private data */
  2608. usb_set_intfdata(interface, NULL);
  2609. }
  2610. static void async_get_intf(struct work_struct *data)
  2611. {
  2612. struct hso_device *hso_dev =
  2613. container_of(data, struct hso_device, async_get_intf);
  2614. usb_autopm_get_interface(hso_dev->interface);
  2615. }
  2616. static void async_put_intf(struct work_struct *data)
  2617. {
  2618. struct hso_device *hso_dev =
  2619. container_of(data, struct hso_device, async_put_intf);
  2620. usb_autopm_put_interface(hso_dev->interface);
  2621. }
  2622. static int hso_get_activity(struct hso_device *hso_dev)
  2623. {
  2624. if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
  2625. if (!hso_dev->is_active) {
  2626. hso_dev->is_active = 1;
  2627. schedule_work(&hso_dev->async_get_intf);
  2628. }
  2629. }
  2630. if (hso_dev->usb->state != USB_STATE_CONFIGURED)
  2631. return -EAGAIN;
  2632. usb_mark_last_busy(hso_dev->usb);
  2633. return 0;
  2634. }
  2635. static int hso_put_activity(struct hso_device *hso_dev)
  2636. {
  2637. if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
  2638. if (hso_dev->is_active) {
  2639. hso_dev->is_active = 0;
  2640. schedule_work(&hso_dev->async_put_intf);
  2641. return -EAGAIN;
  2642. }
  2643. }
  2644. hso_dev->is_active = 0;
  2645. return 0;
  2646. }
  2647. /* called by kernel when we need to suspend device */
  2648. static int hso_suspend(struct usb_interface *iface, pm_message_t message)
  2649. {
  2650. int i, result;
  2651. /* Stop all serial ports */
  2652. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2653. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2654. result = hso_stop_serial_device(serial_table[i]);
  2655. if (result)
  2656. goto out;
  2657. }
  2658. }
  2659. /* Stop all network ports */
  2660. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2661. if (network_table[i] &&
  2662. (network_table[i]->interface == iface)) {
  2663. result = hso_stop_net_device(network_table[i]);
  2664. if (result)
  2665. goto out;
  2666. }
  2667. }
  2668. out:
  2669. return 0;
  2670. }
  2671. /* called by kernel when we need to resume device */
  2672. static int hso_resume(struct usb_interface *iface)
  2673. {
  2674. int i, result = 0;
  2675. struct hso_net *hso_net;
  2676. /* Start all serial ports */
  2677. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2678. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2679. if (dev2ser(serial_table[i])->open_count) {
  2680. result =
  2681. hso_start_serial_device(serial_table[i], GFP_NOIO);
  2682. hso_kick_transmit(dev2ser(serial_table[i]));
  2683. if (result)
  2684. goto out;
  2685. }
  2686. }
  2687. }
  2688. /* Start all network ports */
  2689. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2690. if (network_table[i] &&
  2691. (network_table[i]->interface == iface)) {
  2692. hso_net = dev2net(network_table[i]);
  2693. if (hso_net->flags & IFF_UP) {
  2694. /* First transmit any lingering data,
  2695. then restart the device. */
  2696. if (hso_net->skb_tx_buf) {
  2697. dev_dbg(&iface->dev,
  2698. "Transmitting"
  2699. " lingering data\n");
  2700. hso_net_start_xmit(hso_net->skb_tx_buf,
  2701. hso_net->net);
  2702. hso_net->skb_tx_buf = NULL;
  2703. }
  2704. result = hso_start_net_device(network_table[i]);
  2705. if (result)
  2706. goto out;
  2707. }
  2708. }
  2709. }
  2710. out:
  2711. return result;
  2712. }
  2713. static void reset_device(struct work_struct *data)
  2714. {
  2715. struct hso_device *hso_dev =
  2716. container_of(data, struct hso_device, reset_device);
  2717. struct usb_device *usb = hso_dev->usb;
  2718. int result;
  2719. if (hso_dev->usb_gone) {
  2720. D1("No reset during disconnect\n");
  2721. } else {
  2722. result = usb_lock_device_for_reset(usb, hso_dev->interface);
  2723. if (result < 0)
  2724. D1("unable to lock device for reset: %d\n", result);
  2725. else {
  2726. usb_reset_device(usb);
  2727. usb_unlock_device(usb);
  2728. }
  2729. }
  2730. }
  2731. static void hso_serial_ref_free(struct kref *ref)
  2732. {
  2733. struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
  2734. hso_free_serial_device(hso_dev);
  2735. }
  2736. static void hso_free_interface(struct usb_interface *interface)
  2737. {
  2738. struct hso_serial *hso_dev;
  2739. struct tty_struct *tty;
  2740. int i;
  2741. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2742. if (serial_table[i] &&
  2743. (serial_table[i]->interface == interface)) {
  2744. hso_dev = dev2ser(serial_table[i]);
  2745. spin_lock_irq(&hso_dev->serial_lock);
  2746. tty = tty_kref_get(hso_dev->tty);
  2747. spin_unlock_irq(&hso_dev->serial_lock);
  2748. if (tty)
  2749. tty_hangup(tty);
  2750. mutex_lock(&hso_dev->parent->mutex);
  2751. tty_kref_put(tty);
  2752. hso_dev->parent->usb_gone = 1;
  2753. mutex_unlock(&hso_dev->parent->mutex);
  2754. kref_put(&serial_table[i]->ref, hso_serial_ref_free);
  2755. }
  2756. }
  2757. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2758. if (network_table[i] &&
  2759. (network_table[i]->interface == interface)) {
  2760. struct rfkill *rfk = dev2net(network_table[i])->rfkill;
  2761. /* hso_stop_net_device doesn't stop the net queue since
  2762. * traffic needs to start it again when suspended */
  2763. netif_stop_queue(dev2net(network_table[i])->net);
  2764. hso_stop_net_device(network_table[i]);
  2765. cancel_work_sync(&network_table[i]->async_put_intf);
  2766. cancel_work_sync(&network_table[i]->async_get_intf);
  2767. if (rfk) {
  2768. rfkill_unregister(rfk);
  2769. rfkill_destroy(rfk);
  2770. }
  2771. hso_free_net_device(network_table[i]);
  2772. }
  2773. }
  2774. }
  2775. /* Helper functions */
  2776. /* Get the endpoint ! */
  2777. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  2778. int type, int dir)
  2779. {
  2780. int i;
  2781. struct usb_host_interface *iface = intf->cur_altsetting;
  2782. struct usb_endpoint_descriptor *endp;
  2783. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2784. endp = &iface->endpoint[i].desc;
  2785. if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
  2786. (usb_endpoint_type(endp) == type))
  2787. return endp;
  2788. }
  2789. return NULL;
  2790. }
  2791. /* Get the byte that describes which ports are enabled */
  2792. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
  2793. {
  2794. int i;
  2795. struct usb_host_interface *iface = intf->cur_altsetting;
  2796. if (iface->extralen == 3) {
  2797. *ports = iface->extra[2];
  2798. return 0;
  2799. }
  2800. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2801. if (iface->endpoint[i].extralen == 3) {
  2802. *ports = iface->endpoint[i].extra[2];
  2803. return 0;
  2804. }
  2805. }
  2806. return -1;
  2807. }
  2808. /* interrupt urb needs to be submitted, used for serial read of muxed port */
  2809. static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
  2810. struct usb_device *usb, gfp_t gfp)
  2811. {
  2812. int result;
  2813. usb_fill_int_urb(shared_int->shared_intr_urb, usb,
  2814. usb_rcvintpipe(usb,
  2815. shared_int->intr_endp->bEndpointAddress & 0x7F),
  2816. shared_int->shared_intr_buf,
  2817. 1,
  2818. intr_callback, shared_int,
  2819. shared_int->intr_endp->bInterval);
  2820. result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
  2821. if (result)
  2822. dev_warn(&usb->dev, "%s failed mux_intr_urb %d\n", __func__,
  2823. result);
  2824. return result;
  2825. }
  2826. /* operations setup of the serial interface */
  2827. static const struct tty_operations hso_serial_ops = {
  2828. .open = hso_serial_open,
  2829. .close = hso_serial_close,
  2830. .write = hso_serial_write,
  2831. .write_room = hso_serial_write_room,
  2832. .ioctl = hso_serial_ioctl,
  2833. .set_termios = hso_serial_set_termios,
  2834. .chars_in_buffer = hso_serial_chars_in_buffer,
  2835. .tiocmget = hso_serial_tiocmget,
  2836. .tiocmset = hso_serial_tiocmset,
  2837. .unthrottle = hso_unthrottle
  2838. };
  2839. static struct usb_driver hso_driver = {
  2840. .name = driver_name,
  2841. .probe = hso_probe,
  2842. .disconnect = hso_disconnect,
  2843. .id_table = hso_ids,
  2844. .suspend = hso_suspend,
  2845. .resume = hso_resume,
  2846. .reset_resume = hso_resume,
  2847. .supports_autosuspend = 1,
  2848. };
  2849. static int __init hso_init(void)
  2850. {
  2851. int i;
  2852. int result;
  2853. /* put it in the log */
  2854. printk(KERN_INFO "hso: %s\n", version);
  2855. /* Initialise the serial table semaphore and table */
  2856. spin_lock_init(&serial_table_lock);
  2857. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
  2858. serial_table[i] = NULL;
  2859. /* allocate our driver using the proper amount of supported minors */
  2860. tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
  2861. if (!tty_drv)
  2862. return -ENOMEM;
  2863. /* fill in all needed values */
  2864. tty_drv->magic = TTY_DRIVER_MAGIC;
  2865. tty_drv->owner = THIS_MODULE;
  2866. tty_drv->driver_name = driver_name;
  2867. tty_drv->name = tty_filename;
  2868. /* if major number is provided as parameter, use that one */
  2869. if (tty_major)
  2870. tty_drv->major = tty_major;
  2871. tty_drv->minor_start = 0;
  2872. tty_drv->num = HSO_SERIAL_TTY_MINORS;
  2873. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  2874. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  2875. tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  2876. tty_drv->init_termios = tty_std_termios;
  2877. hso_init_termios(&tty_drv->init_termios);
  2878. tty_set_operations(tty_drv, &hso_serial_ops);
  2879. /* register the tty driver */
  2880. result = tty_register_driver(tty_drv);
  2881. if (result) {
  2882. printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
  2883. __func__, result);
  2884. return result;
  2885. }
  2886. /* register this module as an usb driver */
  2887. result = usb_register(&hso_driver);
  2888. if (result) {
  2889. printk(KERN_ERR "Could not register hso driver? error: %d\n",
  2890. result);
  2891. /* cleanup serial interface */
  2892. tty_unregister_driver(tty_drv);
  2893. return result;
  2894. }
  2895. /* done */
  2896. return 0;
  2897. }
  2898. static void __exit hso_exit(void)
  2899. {
  2900. printk(KERN_INFO "hso: unloaded\n");
  2901. tty_unregister_driver(tty_drv);
  2902. /* deregister the usb driver */
  2903. usb_deregister(&hso_driver);
  2904. }
  2905. /* Module definitions */
  2906. module_init(hso_init);
  2907. module_exit(hso_exit);
  2908. MODULE_AUTHOR(MOD_AUTHOR);
  2909. MODULE_DESCRIPTION(MOD_DESCRIPTION);
  2910. MODULE_LICENSE(MOD_LICENSE);
  2911. /* change the debug level (eg: insmod hso.ko debug=0x04) */
  2912. MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
  2913. module_param(debug, int, S_IRUGO | S_IWUSR);
  2914. /* set the major tty number (eg: insmod hso.ko tty_major=245) */
  2915. MODULE_PARM_DESC(tty_major, "Set the major tty number");
  2916. module_param(tty_major, int, S_IRUGO | S_IWUSR);
  2917. /* disable network interface (eg: insmod hso.ko disable_net=1) */
  2918. MODULE_PARM_DESC(disable_net, "Disable the network interface");
  2919. module_param(disable_net, int, S_IRUGO | S_IWUSR);