hso.c 83 KB

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