hso.c 85 KB

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