hso.c 83 KB

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